Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C++ - MAP


A Map is a Collection that can also hold multiple values in Key and Value pairs. In the Map, the elements are unindexed, changeable and doesn't allow duplicate values as Key.


You can think of a Map like an actual English Dictionaries. Where you search for a word and you get its explanation.


The declaration of a Map is also quite simple. You can place the multiple values inside braces {} in the form of Key and Value pairs.


Just imagine the Key to be the word you are going to search in the English Dictionary. And the Value is the explanation you find in it.


Creating a 'Map'


Example :



#include <iostream>
#include <map>

using namespace std;

int main() {

    map<string, string> x = 
        {
	        { "Five", "Is a Number" }, 
	        { "John", "Is a Name" }, 
	        { "C++", "Is a Language"}
        };
        
    for (auto i : x)  
    {  
        cout << "The Key is : " << i.first << " and the Value is :"  << i.second << endl;  
    }
    
    return 0;    
}


Output :



  The Key is : C++ and the Value is :Is a Language
  The Key is : Five and the Value is :Is a Number
  The Key is : John and the Value is :Is a Name

So, in the above code we have created a Map using square braces {}. And the values are represented in Key and Value pairs.


The Key is a number or a String and in the value we have provided its explanation(Somewhat like the English Dictionary).


i.e. We know that the first element Five is a number. So, in the Key, we have put the number Five and in its value, we have put the explanation, Is a Number. Same logic is applicable for the other two Keys, John and C++.


A Key and Value is separated by ,.


map<string, string> x =
	{
		{ "Five", "Is a Number" },
		{ "John", "Is a Name" },
		{ "C++", "Is a Language"}
	};

And initialised to the variable x.

java_Collections

You can also declare a Map in a single line,


map<string, string> x = { { "Five", "Is a Number" }, { "John", "Is a Name" }, { "C++", "Is a Language"}};

Just that to give it a good structure, we have represented it in the below way,


map<string, string> x =
	{
		{ "Five", "Is a Number" },
		{ "John", "Is a Name" },
		{ "C++", "Is a Language"}
	};

In the next line we have used the for range loop to Iterate through the Map.


for (auto i : x)
{
	cout << "The Key is : " << i.first << " and the Value is :"  << i.second << endl;
}

We have used auto type specifier in the for range loop to iterate the Map.


And as we know, there is a Key and Value pair in a Map.


Now, if you look at the for range loop. There are two sections in the for loop.


for (auto i : x)

The first section is,


auto i

And the second section is,


x
java_Collections


So, in the above for loop, the auto i, creates a variable i that would take the Key and Value from Map x and print them one by one.


Now, if you check the print statement inside the for loop,


for (auto i : x)
{
	cout << "The Key is : " << i.first << " and the Value is :"  << i.second << endl;
}

We got the Key using, i.first and the value using i.second.


Let us understand it in the iterations of for loop,


for (auto i : x)

1st Iteration


In the first Iteration the first Key and Value of the Map x (i.e. The key Five and value Is a Number) is taken and put into the variables i.

java_Collections

And the print statement,


cout << "The key is : "+i.Key+" and the value is : "+i.Value);

Prints the key and value of i(i.e. The Key and the Value).

Output :



  The key is : Five and the value is : Is a Number

2nd Iteration


Similarly, in the second Iteration the second Key and Value of the Map x (i.e. The key John and Value Is a Name) is taken and put into the variables i.

java_Collections

And the print statement, prints the value of i(i.e. The key).

Output :



  The key is : Five and the value is : Is a Number
  The key is : John and the value is : Is a Name

3rd Iteration


Similarly, in the third Iteration the third Key and Value of the Map x (i.e. The key C++ and the value Is a Language) is taken and put into the variables i.

java_Collections

And the print statement, prints the value of i(i.e. The key).

Output :



  The key is : Five and the value is : Is a Number
  The key is : John and the value is : Is a Name
  The key is : C++ and the value is : Is a Language

So, we have seen how to get the Keys. Now, let us see how can we get the corresponding Value associated with that Key.


It is just like searching a word in an English dictionary and get its corresponding value.