Map Operator

C++ map operator function is used to access the elements in the map with the given key value .

It is similar to the at function. Only difference between them is that at throws an exception if the accessed key is not present in the map, on the other hand operator inserts the key in the map if the key is not present already in the map.

Syntax

Consider the key value k , syntax would be:

Example

mapped_type& operator[] (const key_type& k);    //until C++ 11
mapped_type& operator[] (const key_type& k);   //since C++ 11
mapped_type& operator[] (key_type&& k); //since C++ 11

Parameter

k : Key value of the element whose mapped value is accessed.

Return value

It returns a reference to the mapped value of the element with a key value.

Example 1

Let's see a simple example for accessing the elements.

Example

#include <iostream>
#include <map>
using namespace std;
int main() 
{
  
   map<char, int> m = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5},
            };

   cout << "Map contains following elements" << endl;

   cout << "m['a'] = " << m['a'] << endl;
   cout << "m['b'] = " << m['b'] << endl;
   cout << "m['c'] = " << m['c'] << endl;
   cout << "m['d'] = " << m['d'] << endl;
   cout << "m['e'] = " << m['e'] << endl;

   return 0;
}

Output:

Output

Map contains following elements
m['a'] = 1
m['b'] = 2
m['c'] = 3
m['d'] = 4
m['e'] = 5

In the above, operator function is used for accessing the elements of map.

Example 2

Let's see a simple example to add the elements using their key values.

Example

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main ()
{
  map<int,string> mymap = {
                { 101, "" },
                { 102, "" },
                { 103, ""} };

  mymap[101] = "Java"; 
  mymap[102] = "T";
  mymap[103] = "Point";

		// prints value associated with key 101, i.e. Java
  cout<<mymap[101]; 
          // prints value associated with key 102, i.e T
  cout<<mymap[102];
          // prints value associated with key 103, i.e Point	
  cout<<mymap[103];

  return 0;
}

Output:

Output

Cpp Tutorial

In the above example, operator is used to add the elements after initialization using the associated key values.

Example 3

Let's see a simple example to change the value associated with the key value.

Example

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main ()
{
  map<int,string> mymap = {
                { 100, "Nikita"},
                { 200, "Deep"  },
                { 300, "Priya" },
                { 400, "Suman" },
                { 500, "Aman"  }};
                
  cout<<"Elements are:" <<endl;
  for (auto& x: mymap) {
    	cout << x.first << ": " << x.second << '\n';
  }

  mymap[100] = "Nidhi"; // changes the value associated with key 100 to Nidhi
  mymap[300] = "Pinku"; // changes the value associated with key 300 to Pinku
  mymap[500] = "Arohi"; // changes the value associated with key 500 to Arohi
  
  
  cout<<"\nElements after make changes are:" <<endl;
  for (auto& x: mymap) {
    	cout << x.first << ": " << x.second << '\n';
  }
  
  return 0;
}

Output:

Output

Elements are:
100: Nikita
200: Deep
300: Priya
400: Suman
500: Aman

Elements after make changes are:
100: Nidhi
200: Deep
300: Pinku
400: Suman
500: Arohi

In the above example, operator function is used to change the values associated with their key values.

Example 4

Let's see a simple example to differentiate between operator and at.

Example

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main ()
{
  map<char,string> mp = {
                { 'a',"Java"},
                { 'b', "C++"  },
                { 'c', "Python" }};
            
    cout<<endl<<mp['a'];
    cout<<endl<<mp['b'];
    cout<<endl<<mp['c'];
    
     mp['d'] = "SQL";   
  /* since there is no key with value 'd' in the map, 
        it insert a key-value pair in map with key 'd' and value = "SQL" */
        
     cout<<endl<<mp['d'];
     
    try {
        mp.at('z'); 
          // since there is no key with value z in the map, it throws an exception 
         
        
    } catch(const out_of_range &e) {
        cout<<endl<<"\nOut of Range Exception at "<<e.what();
    }
return 0;
}

Output:

Output

Java
C++
Python
SQL

Out of Range Exception at map::at

In the above example, when we are using at function it throws an outofrange Exception since there is no key with value z in the map and when we use operator and add an element in key value d since, there is no key with value 'd' in the map, it insert a key-value pair in map with key 'd' and value = "SQL".

Input Required

This code uses input(). Please provide values below: