映射运算符 [] 操作数

发布于 2024-08-31 21:40:41 字数 1521 浏览 6 评论 0原文

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

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); 

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

Hi all I have the following in a member function

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

and the following declaration of a member variable

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

However I get an error when compiling (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>                                                                    ^

I know it's got to be something silly but I can't see what I've done wrong.

UPDATE I'm calling this from a const member function which is why the member variable's type is const so I thought that something like the following should fix it:

int dest = 0, tt = 6; 
const set<int>& egressCandidateStops = m_egressCandidatesByDestAndOtMode[tt].at(dest); 

But no dice... still the same error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

携余温的黄昏 2024-09-07 21:40:41

操作数类型为:const std::map<整数...

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

回答了 这是几天前的事。

map::operator[] 有点奇怪。它
这样做:

  1. 寻找钥匙。
  2. 如果找到,请将其归还。
  3. 如果没有,则插入它并默认构造其关联的
    值。
  4. 然后返回对新值的引用。

步骤 3 与常量不兼容。
而不是拥有两个
不同功能的运算符[]
超载,语言迫使你
使用 map::find 来查找 const 对象。

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

map::operator[] does not work with a const map.

I answered this a few days ago.

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.

多像笑话 2024-09-07 21:40:41

[] 的原型是

 data_type& operator[](const key_type& k)

一个非常量操作,因此您不能在 const 成员函数的成员上调用它。

您可以将代码更改为:

std::map<...>::const_iterator where = m_egressCandidatesByDestAndOtMode.find(tt);
if (egressCandidatesByDestAndOtMode.end() != where) {
    const vector<set<int>>& temp = where->second;
}

The prototype for [] is

 data_type& operator[](const key_type& k)

i.e. a non const operation, so you can't call it on a member from a const member function .

You could change the code to:

std::map<...>::const_iterator where = m_egressCandidatesByDestAndOtMode.find(tt);
if (egressCandidatesByDestAndOtMode.end() != where) {
    const vector<set<int>>& temp = where->second;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文