简体   繁体   English

为什么std :: map没有const访问器?

[英]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和非constfind )。 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. (非constoperator[]创建密钥(如果它不存在)。

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.

相关问题 为什么 std::map 没有 FindOrNull 方法 - Why std::map does not have a FindOrNull method 为什么 std::string 没有(显式) const char* 强制转换 - Why std::string does not have (explicit) const char* cast 为什么 std::unique_ptr 没有 const get 方法? - Why does std::unique_ptr not have a const get method? 为什么为 const std::vector 定义了 operator[],而不为 const std::map 定义了 operator[]? - Why is operator[] defined for const std::vector, but not for const std::map? C++:为什么在 const std::pair 中只有键必须是 const? - C++: Why does only the key have to be const in a const std::pair? 为什么 std::unordered_map 不适用于 const std::string 键? - Why does std::unordered_map not work with const std::string key? 为什么std :: map :: const_iterator在std :: for_each期间调用std :: pair构造函数,但是一个简单的for循环却没有? - Why does std::map::const_iterator call the std::pair constructor during a std::for_each, but a simple for loop does not? 为什么std :: unordered_map有一个保留方法? - Why does std::unordered_map have a reserve method? 为什么std :: map有一个find成员函数? - Why does std::map have a find member function? 为什么+ =在没有值的std :: map键上起作用? - Why does += work on std::map keys that don't have values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM