题
大家好,我在会员功能中都有以下内容
int tt = 6;
vector<set<int>>& temp = m_egressCandidatesByDestAndOtMode[tt];
set<int>& egressCandidateStops = temp.at(dest);
以及以下会员变量的声明
map<int, vector<set<int>>> m_egressCandidatesByDestAndOtMode;
但是,我在编译时会遇到错误(英特尔编译器11.0)
1>C:\projects\svn\bdk\Source\ZenithAssignment\src\Iteration\PtBranchAndBoundIterationOriginRunner.cpp(85): error: no operator "[]" matches these operands
1> operand types are: const std::map<int, std::vector<std::set<int, std::less<int>, std::allocator<int>>, std::allocator<std::set<int, std::less<int>, std::allocator<int>>>>, std::less<int>, std::allocator<std::pair<const int, std::vector<std::set<int, std::less<int>, std::allocator<int>>, std::allocator<std::set<int, std::less<int>, std::allocator<int>>>>>>> [ const int ]
1> vector<set<int>>& temp = m_egressCandidatesByDestAndOtMode[tt];
1> ^
我知道这一定是愚蠢的,但我看不到我做错了什么。
更新 我是从const成员函数中调用的,这就是为什么成员变量的类型为const的原因,所以我认为类似以下内容应该解决它:
int dest = 0, tt = 6;
const set<int>& egressCandidateStops = m_egressCandidatesByDestAndOtMode[tt].at(dest);
但是没有骰子...仍然有同样的错误。
解决方案
操作数类型是: const std ::地图<int…
map::operator[]
不适合 const map
.
我 回答 几天前。
MAP ::操作员[]有点奇怪。它这样做:
- 寻找钥匙。
- 如果发现,请退还。
- 如果不是,请插入并默认构造其关联的值。
- 然后返回对新值的引用。
步骤3与constness不相容。该语言没有两个不同功能的操作员[]过载,而是迫使您使用map :: for const对象。
其他提示
原型 []
是
data_type& operator[](const key_type& k)
即非const操作,因此您无法将其调用来自const成员函数的成员。
您可以将代码更改为:
std::map<...>::const_iterator where = m_egressCandidatesByDestAndOtMode.find(tt);
if (egressCandidatesByDestAndOtMode.end() != where) {
const vector<set<int>>& temp = where->second;
}
不隶属于 StackOverflow