无法读取const std :: map<>& [英] unable to read from const std::map<>&

查看:94
本文介绍了无法读取const std :: map<>&的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其实我很确定我在这里错过了一些微不足道的东西,但我找不到它。

貌似我无法读取const地图&


我试试

展开 | 选择 | Wrap | 行号

解决方案

const有与它无关。


这是你在第9行的cout中使用operator []。该运算符返回对map元素的引用。可以通过使用此引用来更改地图。你的const会妨碍你的编译错误。


要么:a)不要使用const或b)使用const但不要使用地图[] operator。



const与它无关。


它'你在第9行的cout中使用了operator []。该运算符返回对map元素的引用。可以通过使用此引用来更改地图。你的const妨碍了你,你得到一个编译错误。



ok


要么:a)不要使用const



mhh,我真的不觉得这是一个解决方案,而是一种我觉得不舒服的解决方法。地图是在一个地方创建的,我想把它交给它并从多个地方读取而无法改变它。



或b)使用const但不要使用map []运算符。



但据我所知,访问地图中元素的唯一方法是const_iterator,它不适合我 - 我宁愿需要一些随机访问。


好​​吧,谢谢你的回答。

也许这里的地图不是正确的 - 我会寻找别的东西。


谢谢。


< blockquote>为什么坚持map :: operator [] ???


您可以使用map :: find()代替。这将返回具有您的键和值的对。


否则,您可以编写一个新的地图iof您自己从地图派生(bs确定地图有一个虚拟析构函数)然后在地图中写一个operator [],返回键的const值而不是const_iterator。


请注意,地图中的访问不是随机的,就像数组一样。访问权限基于密钥而非元素编号。 map :: operator []尝试使地图看起来像一个数组,但它确实以允许你改变它的形式返回值。


Actually I''m quite sure I''ve missed something trivial here, but I just can''t find it.
Seemingly I cannot read from a const map&

I try

Expand|Select|Wrap|Line Numbers

解决方案

The const has nothing to do with it.

It''s your use of operator[] in the the cout in line 9. That operator returns a reference to the map element. It''s possible to change the map byusing this reference. Your const gets in the way of that and you get a compile error.

Either: a) don''t use const or b) use const but don''t use the map [] operator.


The const has nothing to do with it.

It''s your use of operator[] in the the cout in line 9. That operator returns a reference to the map element. It''s possible to change the map byusing this reference. Your const gets in the way of that and you get a compile error.

ok

Either: a) don''t use const

mhh, I dont really feel like this is a solution, but rather a workaround which I dont feel comfortable with. The map is created in one spot and I want to hand it around and read it from multiple places without the possibility to change it.


or b) use const but don''t use the map [] operator.

But as far as I can see the only other way to access the elements in the map then is the const_iterator which is not suitable for me - I rather would need some kind of random access.

Well, ok thanks for your answer.
Maybe map is just not the right thing here - I''ll look for something else.

Thanks.


Why insist on map::operator[] ???

You can use map::find() instead. That will return the pair that has your key and value.

Otherwise, you can write a new map iof your own be deriving from map (bs sure map has a virtual destructor) and then write an operator[] in your map that returns a const value for the key rather than a const_iterator.

Please note, access in a map is not random, like an array. Access is based on a key and not and element number. The map::operator[] tries to make the map look like an array but it does return the value in a form that allows you to change it.


这篇关于无法读取const std :: map&lt;&gt;&amp;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆