C++ const std::map 引用无法编译
为什么将 std::map 的引用作为 const 传递会导致 [] 运算符中断? 当我使用 const 时,出现此编译器错误 (gcc 4.2):
错误:与“operator[]”不匹配 '地图[名称]'
这是函数原型:
void func(const char ch, std::string &str, const std::map<std::string, std::string> &map);
而且,我应该提到,当我删除 std::map
前面的 const
关键字时,没有问题。
如果我的指示正确,[] 运算符如果找不到密钥,实际上会在映射中插入一个新对,这当然可以解释为什么会发生这种情况,但我无法想象这会是可接受的行为。
如果有更好的方法,例如使用 find 而不是 [],我将不胜感激。 我似乎也无法找到工作...我收到 const 不匹配的迭代器错误。
Is there a reason why passing a reference to a std::map
as const causes the [] operator to break? I get this compiler error (gcc 4.2) when I use const:
error: no match for ‘operator[]’ in
‘map[name]’
Here's the function prototype:
void func(const char ch, std::string &str, const std::map<std::string, std::string> &map);
And, I should mention that there is no problem when I remove the const
keyword in front of std::map
.
If I've been instructed correctly, the [] operator will actually insert a new pair into the map if it doesn't find the key, which would of course explain why this happens, but I can't imagine that this would ever be acceptable behavior.
If there is a better method, like using find instead of [], I'd appreciate it. I can't seem to get find to work either though... I receive const mismatched iterator errors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,您不能使用
operator[]
。 使用find
,但请注意它返回const_iterator
而不是iterator
:这就像指针一样。 您不能将
int const*
分配给int*
。 同样,您不能将const_iterator
分配给iterator
。Yes you can't use
operator[]
. Usefind
, but note it returnsconst_iterator
instead ofiterator
:It's like with pointers. You can't assign
int const*
toint*
. Likewise, you can't assignconst_iterator
toiterator
.当您使用运算符[]时,std::map 会查找具有给定键的项目。 如果没有找到,它就会创建它。 这就是 const 的问题。
使用 find 方法就可以了。
您能否发布有关如何尝试使用 find() 的代码?
正确的方法是:
When you're using operator[], std::map looks for item with given key. If it does not find any, it creates it. Hence the problem with const.
Use find method and you'll be fine.
Can you please post code on how you're trying to use find() ?
The right way would be :
如果您使用的是 C++11,std::map::at 应该适合你。
原因 std::map::operator[] 不不起作用的是,如果您要查找的键在地图中不存在,它将使用提供的键插入一个新元素并返回对其的引用(有关详细信息,请参阅链接)。 这在 const std::map 上是不可能的。
但是,如果键不存在,“at”方法将引发异常。 话虽如此,在尝试使用 'at' 方法访问元素之前,使用 std::map::find 方法检查键是否存在可能是一个好主意。
If you're using C++11, std::map::at should work for you.
The reason std::map::operator[] doesn't work is that in the event of the key you're looking for not existing in the map, it will insert a new element using the provided key and return a reference to it (See the link for details). This is not possible on a const std::map.
The 'at' method, however, will throw an exception if the key doesn't exist. That being said, it is probably a good idea to check for the existence of the key using the std::map::find method before attempting to access the element using the 'at' method.
可能是因为 std::map 中没有 const operator[] 。 如果找不到,operator[] 将添加您要查找的元素。 因此,如果您想搜索但无法添加,请使用 find() 方法。
Probably because there is no const operator[] in std::map. operator[] will add the element you're looking for if it doesn't find it. Therefore, use the find() method if you want to search without the possibility of adding.
对于“const 不匹配迭代器错误”:
find() 有两个重载:
我的猜测是您收到此错误是因为您正在执行诸如分配非常量迭代器之类的操作(在左侧) ) 到
const
映射上的find()
调用的结果:这会导致错误,尽管可能不是您所看到的错误。
For your "const mismatched iterator errors" :
find() has two overloads:
My guess is that you're getting this error because you're doing something like assigning a non-const iterator (on the left) to the result of a
find()
call on aconst
map:That would result in an error, though perhaps not the one you're seeing.