map is a container that stores elements in key-value pairs. It's similar to collections in Java, associative arrays in PHP, or objects in JavaScript.

Here are the main benefits of using map:

  • map only stores unique keys, and the keys themselves are in sorted order
  • Because the keys are already in order, searching for an element is very fast
  • There is only one value for every key

Here is an example:

#include <iostream>
#include <map>

using namespace std;

int main (){
  map<char,int> first;
  
  //initializing
  first['a']=10;
  first['b']=20;
  first['c']=30;
  first['d']=40;
  
   map<char, int>::iterator it;
   for(it=first.begin(); it!=first.end(); ++it){
      cout << it->first << " => " << it->second << '\n';
   }
   
  return 0;
}

Output:

a => 10
b => 20
c => 30
d => 40

Creating a map object

map<string, int> myMap;

Insertion

Inserting data with insert member function.

myMap.insert(make_pair("earth", 1));
myMap.insert(make_pair("moon", 2));

We can also insert data in std::map using operator [] i.e.

myMap["sun"] = 3;

Accessing map elements

To access map elements, you have to create iterator for it. Here is an example as stated before.

map<char, int>::iterator it;
for(it=first.begin(); it!=first.end(); ++it){
  cout << it->first << " => " << it->second << '\n';
}