map<int, 向量<int> > 图 和 graph[i].begin() 无法解析

map<int, vector<int> > graph and graph[i].begin() cannot be resolved

本文关键字:gt int lt begin graph 向量 map      更新时间:2023-10-16

我有这个问题,我有一个map<int, vector<int> > graph我试图访问graph[i].begin()int i = 0;,它无法解决。

错误是:

error: no viable overloaded operator[] for type 'const map<int, vector<int> >

有人能解释一下吗?谢谢!

operator[]不是const成员,因此不能应用于const map

为什么operator[]是非const?因为如果该元素不存在,它会将其插入到映射中(这会修改映射)。

注意错误信息中的"const"。由于map上的操作符[]将按需创建元素,因此操作符[]需要具有写访问权限。在您的例子中,它不存在,因此非const操作符[]不存在可行。