Map in STL C++

About Map in STL :

Map in STL is defined as the container used to store the elements as the key-value pair. Keys will always be unique in the map

map in stl

Syntax of Map :

map< data_type1, data_type2> map_name;

In STL, An associative container known as a Map is used to store objects in mapped form.
Every object on the map is made up of a key-value pair and an associated value.
The same key values cannot be shared by two mapped values.The key values work well for classifying and single-handedly recognising components.
The mapped values are used to store the key-related material. Although the types of the two may be different, the member type nonetheless combines them using a pair type that combines both.

Functions in Map :

 FunctionWorking
insert({})insert a pair of key – value to the map
count()return the number of matches element from the map.
size()give the number of keys present in the map.
empty()tells whether the map is empty or not.
erase(const it)

remove the element at the position pointed by iterator.

Example of Map in STL :

Run
#include<iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
     
    map< int,int> mpp;
    mpp[1] = 10;
    mpp[2] = 20;
    mpp[3] = 30;
    mpp.insert({4,40});
    mpp.insert({5,50});
    cout<<"The key value pair in map are : "<< endl;
    for(auto it : mpp){
        cout<< it.first << " "<< it.second<< endl;
    }
    return 0;
}

Output :

The key value pair in map are : 
1 10
2 20
3 30
4 40
5 50

In the above program, we take the map, mpp and insert different key value pairs in it and print them on the screen.

Example of Map in STL:

Run
#include<iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
     
    map< int,int> mpp;
    mpp[1] = 10;
    mpp[2] = 20;
    mpp[3] = 30;
    mpp.insert({4,40});
    mpp.insert({5,50});
    mpp[3] = 60;
    cout<<"The key value pair in map are : "<< endl;
    for(auto it : mpp){
        cout<< it.first << " "<< it.second<< endl;
    }
    cout<< endl;
    cout<<"The size of map : "<< mpp.size()<< endl;
    int val = 2;
    // finding  whether value val is present in map or not
    for(auto it : mpp){
        if(it.first == val){
            cout<< val<<" is present in the map";
            return 0;
        }
        
    }
    cout<< val<< " is not present in the map";
    return 0;
}

Output :

The key value pair in map are : 
1 10
2 20
3 60
4 40
5 50

The size of map : 5
2 is present in the map

In the above program, we take the map, mpp and insert different key value pairs in it and print them, also we use iterator based for loop to check if value is present in the map or not.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription