C++ const 映射元素访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5134614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:28:51  来源:igfitidea点击:

C++ const map element access

c++stlmapconst

提问by icephere

I tried to use the operator[] access the element in a const C++ map, but this method failed. I also tried to use "at()" to do the same thing. It worked this time. However, I could not find any reference about using "at()" to access element in a const C++ map. Is "at()" a newly added function in C++ map? Where can I find more info about this? Thank you very much!

我尝试使用 operator[] 访问 const C++ 映射中的元素,但此方法失败。我也尝试使用“at()”来做同样的事情。这次成功了。但是,我找不到有关使用“at()”访问 const C++ 映射中的元素的任何参考。“at()”是C++映射中新增的函数吗?我在哪里可以找到有关此的更多信息?非常感谢!

An example could be the following:

一个例子可能如下:

#include <iostream>
#include <map>

using namespace std;

int main()
{
        map<int, char> A;
        A[1] = 'b';
        A[3] = 'c';

        const map<int, char> B = A;

        cout << B.at(3) << endl; // it works
        cout << B[3] << endl;  // it does not work

}

For using "B[3]", it returned the following errors during compiling:

对于使用“B[3]”,它在编译过程中返回以下错误:

t01.cpp:14: error: passing ‘const std::map, std::allocator > >' as ‘this' argument of ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = int, _Tp = char, _Compare = std::less, _Alloc = std::allocator >]' discards qualifiers

t01.cpp:14: 错误:将 'const std::map, std::allocator > >' 作为 '_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[]( const _Key&) [with _Key = int, _Tp = char, _Compare = std::less, _Alloc = std::allocator >]' 丢弃限定符

The compiler used is g++ 4.2.1

使用的编译器是 g++ 4.2.1

回答by CB Bailey

at()is a new method for std::mapin C++11.

at()std::mapC++11 中的新方法。

Rather than insert a new default constructed element as operator[]does if an element with the given key does not exist, it throws a std::out_of_rangeexception. (This is similar to the behaviour of at()for dequeand vector.)

operator[]如果不存在具有给定键的元素,它不会插入新的默认构造元素,而是抛出std::out_of_range异常。(这类似于at()fordeque和的行为vector。)

Because of this behaviour it makes sense for there to be a constoverload of at(), unlike operator[]which always has the potential to change the map.

由于这种行为,存在 的const过载是有意义的at(),不像operator[]它总是有可能改变地图。

回答by Konrad Rudolph

If an element doesn't exist in a map, the operator []will add it – which obviously cannot work in a constmap so C++ does not define a constversion of the operator. This is a nice example of the compiler's type checker preventing a potential runtime error.

如果 a 中不存在元素mapoperator []则将添加它 - 这显然不能在const映射中工作,因此 C++ 没有定义const运算符的版本。这是编译器类型检查器防止潜在运行时错误的一个很好的例子。

In your case, you need to use findinstead which will onlyreturn an (iterator to the) element if it exists, it will never modify the map. If an item doesn't exist, it returns an iterator to the map's end().

在你的情况,你需要使用find,而不是将如果存在返回(迭代器)元素,它永远不会改变map。如果一个项目不存在,它会返回一个迭代器到地图的end().

atdoesn't exist and shouldn't even compile. Perhaps this is a “compiler extension” (= a bugnew in C++0x).

at不存在,甚至不应该编译。也许这是一个“编译器扩展”(=一个错误C++0x 中的新内容)。

回答by vidstige

The []-operator will create a new entryin the map if the given key does not exists. It may thus change the map.

如果给定的键不存在, [] 运算符将在映射中创建一个新条目。它可能因此改变地图。

See this link.

请参阅此链接

回答by Beta

This comes as quite a surprise to me, but the STL map doesn't have a constindex operator. That is, B[3]cannot be read-only. From the manual:

这让我很惊讶,但 STL 映射没有const索引运算符。也就是说,B[3]不能是只读的。从手册:

Since operator[] might insert a new element into the map, it can't possibly be a const member function.

由于 operator[] 可能会向映射中插入一个新元素,因此它不可能是 const 成员函数。

I have no idea about at().

我不知道at()