[英]Why does std::map not have a const accessor?
The declaration for the [] operator on a std::map is this: std :: map上[]运算符的声明是这样的:
T& operator[] ( const key_type& x );
Is there a reason it isn't this? 有没有理由不是这个?
T& operator[] ( const key_type& x );
const T& operator[] const ( const key_type& x );
Because that would be incredibly useful any time you need to access a member map in a const method. 因为只要您需要在const方法中访问成员映射,这将非常有用。
As of C++11 there is std::map::at
which offers const and non-const access. 从C ++ 11开始,有
std::map::at
,它提供const和非const访问。
In contrast to operator[]
it will throw an std::out_of_range
exception if the element is not in the map. 与
operator[]
相反,如果元素不在map中,它将抛出std::out_of_range
异常。
operator[]
in a map returns the value at the specified key or creates a new value-initialized element for that key if it's not already present, so it would be impossible. map中的
operator[]
返回指定键的值, 或者为该键创建一个新的value-initialized元素(如果它尚未存在),因此这是不可能的。
If operator[]
would have a const
overload, adding the element wouldn't work. 如果
operator[]
具有const
重载,则添加该元素将不起作用。
That answers the question. 这回答了这个问题。 Alternatives:
备择方案:
For C++03 - you can use iterators (these are const
and non- const
coupled with find
). 对于C ++ 03 - 您可以使用迭代器(这些是
const
和非const
与find
)。 In C++11 you can use the at
method. 在C ++ 11中,您可以使用
at
方法。
These answers are correct in that operator[]
has semantics to add a key if it doesn't exist, but I'd like to add another perspective: 这些答案是正确的,因为
operator[]
具有添加密钥的语义(如果它不存在),但我想添加另一个透视图:
Notice how operator[]
returns a T&
. 注意
operator[]
如何返回T&
。 That is, it returns a reference to the value
that is associated with key
. 也就是说,它返回对与
key
关联的value
的引用。 But what if there is no key
in the map
? 但如果
map
没有key
怎么办? What should we return? 我们该怎么回事? There's no such thing as a "null-reference," and throwing an exception would be annoying.
没有“null-reference”这样的东西,抛出异常会很烦人。
This would be one good reason for no operator[] const
. 这将是没有
operator[] const
一个很好的理由。 What would you return to the user if you couldn't add anything to the map
(because the operator is const
), but they are looking for an item that doesn't exist? 如果您无法向
map
添加任何内容(因为运算符是const
),您会返回给用户的内容,但是他们正在寻找不存在的项目? A good solution to this problem is to not have the operator[] const
. 这个问题的一个很好的解决方案是没有
operator[] const
。
The (non- const
) operator[]
creates the key if it doesn't exist. (非
const
) operator[]
创建密钥(如果它不存在)。
The const
version of that operator, if it existed, would have to have different semantics, since it wouldn't be able to add a new key. 该运算符的
const
版本(如果存在)必须具有不同的语义,因为它无法添加新键。
I am sure you would agree that having const
and non- const
overloads with significantly different semantics would be a can of worms. 我相信你会同意使用具有明显不同语义的
const
和非const
重载将成为一堆蠕虫。 Therefore no const
version is provided. 因此,不提供
const
版本。
There is a const find()
member though, so you can use that in your code. 但是有一个const
find()
成员,所以你可以在你的代码中使用它。
因为operator[]
的语义是添加密钥(如果它不存在)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.