How to initialize a map in C++

In this tutorial, we will see how to initialize a map in C++. So, a Map is part of the Standard template library (STL), STL map is an associative container that stores elements in the form of key-value or mapped fashion. In a map, there is no repetition of key-value, because in a map key-value must be unique. After assigning a key-value the map will automatically arrange key-value in sorted form.

Before assigning key-value elements we should include a map header file because a map header file includes all the important functions that we can use in our program to handle the map efficiently.

We will understand how to initialize a map step by step in the below example –

Step 1 – Include map header file

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

The first line and second line start with a pound (#) sign so it is called directives, which tell the compiler to include a header file (i.e. iostream and map) that contains declarations or functions that we can use in our program. The third line (i.e. using namespace std) imports all the entities from the standard namespace to the current namespace of the program.

Step 2 – Syntax for map

int main(){
      map<string,int>mapi;
}

We need to specify this syntax in the int main function before initializing key-value elements. This syntax says that the data type of the key is a string, the data type of value is int and the mapi is the map’s name. Every time we need to specify the data type of key-value elements before initializing them.

Step 3 – How to initialize and print key-value (i.e map) elements –

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

int main(){
    map<string,int>mapi;

    mapi["Bhushan"] = 95;
    mapi["Ashish"] = 92;
    mapi["Abhijeet"] = 98;
    mapi["Hrutwik"] = 91;
    mapi["Abhishek"] = 90;

    for(auto& x: mapi){
        cout<<x.first<<" has "<<x.second<<" marks out of 100"<<endl;
    }
    return 0;
}



Output –

Abhijeet has 98 marks out of 100
Abhishek has 90 marks out of 100
Ashish has 92 marks out of 100
Bhushan has 95 marks out of 100
Hrutwik has 91 marks out of 100

This example shows how to initialize and print key-value elements in a map. In the output section, we see that the map key-value elements are not printed the way we initialize them. Because the map automatically arranges key elements in a sorted manner, we get key-value elements in a sorted manner.

Some other methods to initialize a map in C++

Method 2:

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


int main(){
    map<string,int>mapi = {{"Bhushan",95},{"Ashish",92},{"Abhijeet",98},{"Hrutwik",91},{"Abhishek",90}};


    //mapi = {{"Bhushan",95},{"Ashish",92},{"Abhijeet",98},{"Hrutwik",91},{"Abhishek",90}};
    
    for(auto& x: mapi){
        cout<<x.first<<" has "<<x.second<<" marks out of 100"<<endl;
    }
    return 0;
}

Output –

Abhijeet has 98 marks out of 100
Abhishek has 90 marks out of 100
Ashish has 92 marks out of 100
Bhushan has 95 marks out of 100
Hrutwik has 91 marks out of 100

The above example is an example of how we can initialize a map where we have to write the syntax of the map. This method is also common and mostly used to initialize a map.

Method 3:

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

int main(){
    map<string,int>mapi;

    mapi.insert({"Bhushan",95});
    mapi.insert({"Ashish",92});
    mapi.insert({"Abhijeet",98});
    mapi.insert({"Hrutwik",91});
    mapi.insert({"Abhishek",90});

    for(auto& x: mapi){
        cout<<x.first<<" has "<<x.second<<" marks out of 100"<<endl;
    }
    return 0;
}

Output –

Abhijeet has 98 marks out of 100
Abhishek has 90 marks out of 100
Ashish has 92 marks out of 100
Bhushan has 95 marks out of 100
Hrutwik has 91 marks out of 100

In the above example, we use the insert function to initialize a map. Here insert is a built-in function in the map that is used to insert the map elements at the right position (i.e. according to the sorting form).

Leave a Reply

Your email address will not be published. Required fields are marked *