CodesJava

Easy learning with example program codes

CPP Map

Maps in CPP are used to store sorted key-value pair. They are the associative containers. Each key in a map is unique. CPP facilitates insertion and deletion of a key in a map but do not allow any modifications, however, values can be modified.

Member Functions of a CPP map:

Allocator:

FUNCTIONUSES
get_allocatorTo return an allocator object to construct the map.

 

Capacity:

FUNCTIONUSES
empty()To determine whether the map is empty or not.
max_size()To determine the maximum size of the map.
size()To determine number of elements in the map.

 

Constructor/Destructor:

FUNCTIONUSES
(constructor)To construct a map.
(destructor)To destruct a map.
operator=To copy the elements of the map to another map.

 

Element Access

FUNCTIONUSES
atTo get the element with given key.
operator[]To get the element with given key.

 

Iterators:

FUNCTIONUSES
begin()To point to the first element of the map.
cbegin()To return a constant iterator to the beginning of the map.
cend()To return a constant iterator to the end.
crbegin()To return a const reverse iterator to the beginning.
crend()To return a const reverse iterator to the end.
end()To return an iterator to the end.
rbegin()To return a reverse iterator to the beginning.
rend()To return a reverse iterator to the end.

 

Modifiers:

FUNCTIONUSES
clear()To remove all the elements of the map.
erase()To delete the specified element.
emplace()To construct and insert a new element at a specified position.
emplace_hint()To construct and insert a new element at a specified position by hint.
insert()To insert a new element in the map..
swap()To exchange the contents of two maps.

 

Non-Member Overloaded Functions:

FUNCTIONUSES
operator==To determine whether the two maps are equal or not.
operator!=To determine whether the two maps are equal or not.
operator<=To determine whether the first map is less than or equal to other or not.
operator<To determine whether the first map is less than other or not.
operator>=To determine whether the first map is greater than equal to other or not.
operator>To determine whether the first map is greater than other or not.
swap()To exchange the contents of two maps.

 

Observers:

FUNCTIONUSES
key_compTo return a copy of key comparison object.
value_compTo return a copy of value comparison object.

 

Operations:

FUNCTIONUSES
countTo get the number of elements matching with given key.
equal_rangeTo get the range of elements matches with given key.
findTo find an element with given key.
lower_boundTo get an iterator to lower bound.
upper_boundTo get an iterator to upper bound.

 

Example:

#include <string.h>  
#include <iostream>  
#include <map>  
#include <utility>  
using namespace std;  
int main()  
{  
map<int, string> Students;  
 
Students[1] = "Vibhu";  
Students[2] = "Yash";  
Students[3] = "Abhi";  
Students[4] = "Neelam";  
cout << "Students[2]=" << Students[2] << "\n\n";  
cout << "Map size: " << Students.size() << endl;  
cout << "\nOrder:" << endl;  
for( map<int,string>::iterator i=Students.begin(); i!=Students.end(); ++i)  
{  
cout << (*i).first << ": " << (*i).second << endl;  
}  
cout << "\nReverse Order:" << endl;  
for( map<int,string>::reverse_iterator i=Students.rbegin(); i!=Students.rend(); ++i)  
{  
cout << (*i).first << ": " << (*i).second << endl;  
}  
}

Output

Students[2]=Yash                     
 
Map size: 4       
 
Order:       
1: Vibhu                          
2: Yash                          
3: Abhi                       
4: Neelam        
 
Reverse Order:                         
4: Neelam                                           
3: Abhi                                  
2: Yash                       
1: Vibhu
Please follow and like us:
error
Posted in CPP   








Copyright © 2019 CodesJava DMCA.com Protection Status