C++ const std::map 引用无法编译

发布于 2024-07-16 18:01:39 字数 563 浏览 9 评论 0原文

为什么将 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 技术交流群。

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

发布评论

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

评论(5

太阳哥哥 2024-07-23 18:01:39

是的,您不能使用operator[]。 使用 find,但请注意它返回 const_iterator 而不是 iterator

std::map<std::string, std::string>::const_iterator it;
it = map.find(name);
if(it != map.end()) {
    std::string const& data = it->second;
    // ...
}

这就像指针一样。 您不能将 int const* 分配给 int*。 同样,您不能将 const_iterator 分配给 iterator

Yes you can't use operator[]. Use find, but note it returns const_iterator instead of iterator:

std::map<std::string, std::string>::const_iterator it;
it = map.find(name);
if(it != map.end()) {
    std::string const& data = it->second;
    // ...
}

It's like with pointers. You can't assign int const* to int*. Likewise, you can't assign const_iterator to iterator.

谈场末日恋爱 2024-07-23 18:01:39

当您使用运算符[]时,std::map 会查找具有给定键的项目。 如果没有找到,它就会创建它。 这就是 const 的问题。

使用 find 方法就可以了。

您能否发布有关如何尝试使用 find() 的代码?
正确的方法是:

if( map.find(name) != map.end() )
{
   //...
}

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 :

if( map.find(name) != map.end() )
{
   //...
}
污味仙女 2024-07-23 18:01:39

如果您使用的是 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.

没有你我更好 2024-07-23 18:01:39

可能是因为 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.

寒冷纷飞旳雪 2024-07-23 18:01:39

对于“const 不匹配迭代器错误”:

find() 有两个重载:

      iterator find ( const key_type& x );
const_iterator find ( const key_type& x ) const;

我的猜测是您收到此错误是因为您正在执行诸如分配非常量迭代器之类的操作(在左侧) ) 到 const 映射上的 find() 调用的结果:

iterator<...> myIter /* non-const */ = myConstMap.find(...)

这会导致错误,尽管可能不是您所看到的错误。

For your "const mismatched iterator errors" :

find() has two overloads:

      iterator find ( const key_type& x );
const_iterator find ( const key_type& x ) const;

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 a const map:

iterator<...> myIter /* non-const */ = myConstMap.find(...)

That would result in an error, though perhaps not the one you're seeing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文