Understanding Map in C++
Welcome! In this post, we will see what are maps and the functions of the map along with some examples in C++ programming.
A Map is a container that stores the element in a map fashion. Each element has a key and a corresponding map value.No two map values can have the same key value.
The type of key and corresponding map value may differ.
Map in C++
Some basic functions of the map in C++ are:-
begin()
:-It returns an iterator that points to the first element of a map.end()
:-It returns an iterator that points at the last element of a mapsize()
:-It returns the number of elements in the mapmax_size()
:-It returns the maximum number of elements that a map can holdempty()
:-It returns whether the map is empty or noterase(const k)
:-It removes the key-value k from the maperase(iterator position)
:-It removes the element at the position specified by the iterator- pair insert(key value, map value):-It adds a new element to a map
clear()
:-It deletes all the elements from a map
Now let’s write a code to better explain these.
#include <iostream> #include <iterator> #include <map> using namespace std; int main() { map<int, int> m1; m1.insert(pair<int, int>(1,10)); m1.insert(pair<int, int>(2,20)); m1.insert(pair<int, int>(3,30)); m1.insert(pair<int, int>(4,40)); m1.insert(pair<int, int>(5,50)); m1.insert(pair<int, int>(6,60)); m1.insert(pair<int, int>(7,70)); map<int, int>::iterator itr; cout << "\nThe map m1 is : \n"; cout << "\tKEY\tELEMENT\n"; for (itr=m1.begin();itr!=m1.end();++itr) { cout<<'\t'<< itr->first<<'\t'<<itr->second<<'\n'; } map<int, int> m2(m1.begin(),m1.end()); cout<<"\nThe map m2 after"<<" assign from m1 is : \n"; cout<<"\tKEY\tELEMENT\n"; for(itr=m2.begin();itr!=m2.end();++itr) { cout<<'\t'<<itr->first<<'\t'<<itr->second<<'\n'; } cout<<"\nm2 after removal of elements less than key=2 : \n"; cout<<"\tKEY\tELEMENT\n"; m2.erase(m2.begin(),m2.find(2)); for(itr=m2.begin();itr!=m2.end();++itr) { cout<<'\t'<<itr->first<<'\t'<<itr->second<<'\n'; } int num; num=m2.erase(4); cout<<"\nm2.erase(4): "; cout<<num<<" removed \n"; cout<<"\tKEY\tELEMENT\n"; for (itr=m2.begin();itr!=m2.end();++itr) { cout<<'\t'<<itr->first<<'\t'<<itr->second<<'\n'; } return 0; }
In this code, I have made a map m1 of int, int type.
Then we inserted values in the map using the pair insert() function. Afterward, we declare an iterator itr to the map.
Then we use begin() function to point the iterator to the beginning element of the map
Then we use end()
function to iterate it until the end of the map. And then we print the first and second values contained in the iterator.
Afterward, we create a new map m2 and initialize it to m1 using begin() and end().
Then we print the map m2.
Then we use erase()
, begin()
and find()
function to remove all the elements having a key value less than 2.
At last, we remove the element having key value 4.
Output:-
The map m1 is : KEY ELEMENT 1 10 2 20 3 30 4 40 5 50 6 60 7 70 The map m2 after assign from m1 is : KEY ELEMENT 1 10 2 20 3 30 4 40 5 50 6 60 7 70 m2 after removal of elements less than key=2 : KEY ELEMENT 2 20 3 30 4 40 5 50 6 60 7 70 m2.erase(4): 1 removed KEY ELEMENT 2 20 3 30 5 50 6 60 7 70
This is it, folks! I hope you enjoyed reading and coding it.
Thank You!
Before you go, please check these amazing posts also:
Leave a Reply