gpt4 book ai didi

c++ - 映射运算符 [] 操作数

转载 作者:IT老高 更新时间:2023-10-28 23:10:43 28 4
gpt4 key购买 nike

大家好,我在成员函数中有以下内容

int tt = 6; 
vector<set<int>>& temp = m_egressCandidatesByDestAndOtMode[tt];
set<int>& egressCandidateStops = temp.at(dest);

以及下面的成员变量声明

map<int, vector<set<int>>> m_egressCandidatesByDestAndOtMode;

但是编译时出现错误(Intel Compiler 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);

但是没有骰子...仍然是同样的错误。

最佳答案

operand types are: const std::map< int …

map::operator[] 不适用于 const map

answered这是几天前的。

map::operator[] is a little odd. It does this:

  1. Look for the key.
  2. If found, return it.
  3. If not, insert it and default-construct its associated value.
  4. Then return a reference to the new value.

Step 3 is incompatible with constness. Rather than have two differently-functioning operator[] overloads, the language forces you to use map::find for const objects.

关于c++ - 映射运算符 [] 操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2772190/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com