34

Here is some example code:

 #include<iostream>
 #include<map>
 #include<string>
 using namespace std;

 int main()
 {
   map<char, string> myMap;
   myMap['a'] = "ahh!!";
   cout << myMap['a'] << endl << myMap['b'] << endl;
   return 0;
 }

In this case i am wondering what does myMap['b'] return?

Петър Петров
  • 1,966
  • 1
  • 17
  • 14
Mirza
  • 635
  • 1
  • 9
  • 13

4 Answers4

41

A default constructed std::string ins inserted into the std::map with key 'b' and a reference to that is returned.

It is often useful to consult the documentation, which defines the behavior of operator[] as:

Returns a reference to the object that is associated with a particular key. If the map does not already contain such an object, operator[] inserts the default object data_type().

(The SGI STL documentation is not documentation for the C++ Standard Library, but it is still an invaluable resource as most of the behavior of the Standard Library containers is the same or very close to the behavior of the SGI STL containers.)

Ray Hulha
  • 10,701
  • 5
  • 53
  • 53
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 1
    And (one of) the original author of the STL wrote the SGI documentation I believe. Though I have no proof and that may just be an internet myth. – Martin York Feb 23 '11 at 05:48
  • 1
    That would be Matt Austern. He's been a significant contributor, for a longer period than Stepanov and Lee (who often are considered the original authors). He also wrote [Generic Programming and the STL](http://www.awprofessional.com/catalog/product.asp?product_id={94C46B18-97C4-4143-BA92-E4156187A6D8}) – MSalters Feb 24 '11 at 00:28
  • WHat's the default object? WHere can I find a list of these? For example, is the default integer 0? – trusktr Dec 28 '13 at 23:39
5

A default-constructed object (eg, an empty string in this case) is returned.

This is actually returned even when you say map['a'] = "ahh!!";. The [] operator inserts a default-constructed string at position 'a', and returns a reference to it, which the = operator is then called on.

4

If you try to access a key value using indexing operator [], then 2 things can happen :

  1. The map contains this key. So it will return the corresponding key value
  2. The map doesn't contain the key. In this case it will automatically add a key to the map with key value null.

As 'b' key is not in your map so it will add this key with value ""(empty string) automatically and it will print this empty string.

And here map size will increase by 1

So to look-up for a key you can use .find(), which will return map.end() if the key is not found.
And no extra key will be added automatically

And obviously you can use [] operator when you set a value for a key

Ali Akber
  • 3,670
  • 3
  • 26
  • 40
2

std::map operator[] inserts the default constructed value type in to the map if the key provided for the lookup doesn't exist. So you will get an empty string as the result of the lookup.

Asha
  • 11,002
  • 6
  • 44
  • 66