sách gpt4 ăn đã đi

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

In lại Tác giả: IT Lão Cao 更新时间:2023-10-28 23:10:43 28 4
mua khóa gpt4 giày nike

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

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

以及下面的成员变量声明

map<>>> 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<>, std::allocator>, std::allocator<>, std::allocator>>>, std::less, std::allocator<><>, std::allocator>, std::allocator<>, std::allocator>>>>>> [ const int ]
1> vector<>>& temp = m_egressCandidatesByDestAndOtMode[tt];
1> ^

我知道这一定很愚蠢,但我看不出我做错了什么。

gia hạn我从一个 const 成员函数调用它,这就是为什么成员变量的类型是 const 所以我认为类似下面的东西应该修复它:

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

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

câu trả lời hay nhất

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
Cựu chiến binh CNTT
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress