Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if I read a map's value where the key does not exist?

Tags:

c++

map

map<string, string> dada; dada["dummy"] = "papy"; cout << dada["pootoo"]; 

I'm puzzled because I don't know if it's considered undefined behaviour or not, how to know when I request a key which does not exist, do I just use find instead ?

like image 798
jokoon Avatar asked Apr 12 '12 13:04

jokoon


People also ask

What map return if key is not present?

Check documentation - HashMap get() method description: Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

How do you check if a map contains a key?

containsKey() method is used to check whether a particular key is being mapped into the HashMap or not. It takes the key element as a parameter and returns True if that element is mapped in the map.

What does map return if key not found Golang?

Basics. When you index a map in Go you get two return values; the second one (which is optional) is a boolean that indicates if the key exists. If the key doesn't exist, the first value will be the default zero value.

How do you check if a key is present in a map in Golang?

Check if Key is Present in Map in Golang To check if specific key is present in a given map in Go programming, access the value for the key in map using map[key] expression. This expression returns the value if present, and a boolean value representing if the key is present or not.

How to check if a key exists in a map?

To check if a key exists in a Map, call the has () method on the Map, passing it the name of the key as a parameter. The has method returns true if the specified key exists in the Map, otherwise it returns false. Copied! The only parameter the Map.has method takes is the name of the key to test for presence in the Map.

How to get the key value of a map in Python?

If you try to access a key value using index operator [], then 2 things can happen : The map contains this key. So it will return the corresponding key value. The map doesn't contain the key. In this case, it will automatically add a key to the map with null value. "pootoo" key does't exist in your map.

How to test for presence of a key in a map?

The has method returns true if the specified key exists in the Map, otherwise it returns false. Copied! The only parameter the Map.has method takes is the name of the key to test for presence in the Map.

How to access a key value in a map using index?

If you try to access a key value using index operator [], then 2 things can happen : The map contains this key. So it will return the corresponding key value. The map doesn't contain the key.


1 Answers

The map::operator[] searches the data structure for a value corresponding to the given key, and returns a reference to it.

If it can't find one it transparently creates a default constructed element for it. (If you do not want this behaviour you can use the map::at function instead.)

You can get a full list of methods of std::map here:

http://en.cppreference.com/w/cpp/container/map

Here is the documentation of map::operator[] from the current C++ standard...

23.4.4.3 Map Element Access

T& operator[](const key_type& x);

  1. Effects: If there is no key equivalent to x in the map, inserts value_type(x, T()) into the map.

  2. Requires: key_type shall be CopyConstructible and mapped_type shall be DefaultConstructible.

  3. Returns: A reference to the mapped_type corresponding to x in *this.

  4. Complexity: logarithmic.

T& operator[](key_type&& x);

  1. Effects: If there is no key equivalent to x in the map, inserts value_type(std::move(x), T()) into the map.

  2. Requires: mapped_type shall be DefaultConstructible.

  3. Returns: A reference to the mapped_type corresponding to x in *this.

  4. Complexity: logarithmic.

like image 115
Andrew Tomazos Avatar answered Sep 17 '22 15:09

Andrew Tomazos