C++ map access discards qualifiers (const)

后端 未结 5 692
野性不改
野性不改 2020-12-04 10:05

The following code says that passing the map as const into the operator[] method discards qualifiers:

#include 
#in         


        
相关标签:
5条回答
  • 2020-12-04 10:24

    Since operator[] does not have a const-qualified overload, it cannot be safely used in a const-qualified function. This is probably because the current overload was built with the goal of both returning and setting key values.

    Instead, you can use:

    VALUE = map.find(KEY)->second;
    

    or, in C++11, you can use the at() operator:

    VALUE = map.at(KEY);
    
    0 讨论(0)
  • 2020-12-04 10:24

    Some newer versions of the GCC headers (4.1 and 4.2 on my machine) have non-standard member functions map::at() which are declared const and throw std::out_of_range if the key is not in the map.

    const mapped_type& at(const key_type& __k) const
    

    From a reference in the function's comment, it appears that this has been suggested as a new member function in the standard library.

    0 讨论(0)
  • 2020-12-04 10:25

    You cannot use operator[] on a map that is const as that method is not const as it allows you to modify the map (you can assign to _map[key]). Try using the find method instead.

    0 讨论(0)
  • 2020-12-04 10:39

    std::map's operator [] is not declared as const, and cannot be due to its behavior:

    T& operator[] (const Key& key)

    Returns a reference to the value that is mapped to a key equivalent to key, performing insertion if such key does not already exist.

    As a result, your function cannot be declared const, and use the map's operator[].

    std::map's find() function allows you to look up a key without modifying the map.

    find() returns an iterator, or const_iterator to an std::pair containing both the key (.first) and the value (.second).

    In C++11, you could also use at() for std::map. If element doesn't exist the function throws a std::out_of_range exception, in contrast to operator [].

    0 讨论(0)
  • 2020-12-04 10:39

    First, you should not be using symbols beginning with _ because they are reserved to the language implementation/compiler writer. It would be very easy for _map to be a syntax error on someone's compiler, and you would have no one to blame but yourself.

    If you want to use an underscore, put it at the end, not the beginning. You probably made this mistake because you saw some Microsoft code doing it. Remember, they write their own compiler, so they may be able to get away with it. Even so, it's a bad idea.

    the operator [] not only returns a reference, it actually creates the entry in the map. So you aren't just getting a mapping, if there is none, you are creating one. That's not what you intended.

    0 讨论(0)
提交回复
热议问题