Home >>C++ Standard Template Library Tutorial(STL) >C++ Map

C++ Map

Map in C++

Maps are basically the part of the STL(Standard Template Library).
The associative containers that are used to store sorted key-value pair where each key is unique and apart from alteration, programmers can perform any operation like insertion or deletion is known as map in C++ and the changes can be made in the values associated with keys.

Syntax

Here is the syntax of the map function depicted below:

template <class Key,                                            
           class T,                                                 
           class Compare = less<Key>,                         
           class Alloc = allocator<pair<const Key,T> >  
           > class map;  

Parameter

key : Generally the key data type is stored in the map.

type : The data type of the value can be generally stored in the map.

compare : Two arguments of the same type bool are generally taken by the comparison class and a value is returned. The binary predicate less<"key"> is known to be as the default value and this argument is optional to use.

alloc : This is a type of the allocator object and this argument is generally optional and its default value is the allocator.

Creating a map

In order to create a map in C++ the following statement is generally used:

typedef pair<const Key, T> value_type;  

In order to create a map that has the key of type Key type and value of type value type, the above method is used. The key of a map and the corresponding values that are the most important and ideally inserted as a pair and only key or just a value in a map can’t be inserted in a map.

C++ Map Member Function

Here is the list of all the member functions in maps along with a brief description:

Constructor/Destructor

Function Description
Constructors This is used to construct map
Destructors This is generally used to destructor Map
operator= This function generally copies the elements of the map to another map.

Iterators

Function Description
Begin() This function generally returns an iterator that is pointing towards the first element in the map.
Cbegin() This function generally returns a const iterator that is pointing towards the first element in the map.
End() This function generally returns an iterator that is pointing to the past-end.
Cend() This function generally returns a constant iterator that is pointing to the past-end.
Rbegin() This function generally returns a reverse iterator that is pointing to the end.
Rend() This function generally returns a reverse iterator that is pointing to the beginning.
Crbegin() This function generally returns a constant reverse iterator that is pointing to the end.
Crend() This function generally returns a constant reverse iterator that is pointing to the beginning.

Capacity

Function Description
empty() This function generally returns true if the map is found to be empty.
size() This function generally returns the number of elements that are in the map.
max_size() This function generally returns the maximum size of the map.

Element Access

Function Description
operator[] This function is generally used to retrieve the element with given key.
at() This function is generally used to retrieve the element with given key.

Modifiers

Function Description
insert() This function is generally used to insert element in the map.
erase() This function is generally used to erase elements from the map.
swap() This function is generally used to exchange the content of the map.
clear() This function is generally used to delete all the elements of the map.
emplace() This function is generally used to construct and insert the new elements into the map.
emplace_hint() This function is generally used to construct and insert new elements into the map by the use of hint.

Observers

Function Description
key_comp() This function is generally used to return a copy of the key comparison object.
value_comp() This function is generally used to return a copy of the value comparison object.

Operations

Function Description
find() This function is used to search for an element with the given key.
count() This function is used to gets the number of the elements that matches with the given key.
lower_bound() This function is used to return an iterator to the lower bound.
upper_bound() This function is used to return an iterator to the upper bound.
equal_range() This function is used to return the range of the elements that matches with the given key.

Allocator

Function Description
get_allocator() This function is generally used to return an allocator object that is used to construct the map.

Non-Member Overloaded Functions

Function Description
operator== This function is used check whether the two maps are equal or not.
operator!= This function is used check whether the two maps are equal or not.
operator< This function is used check whether the first map is less than to the other or not.
operator<= This function is used check whether the first map is less than or equal to the other or not.
operator> This function is used check whether the first map is greater than from the other or not.
operator>= This function is used check whether the first map is greater than equal to the other or not.
swap() This function is generally used to exchange the elements of the two maps.

Example of Map Function

Here is a map of students where the student ID is the key and names are the values that can be represented as:

Keys Values
001 kanchan
002 Manish
003 Shipra
004 Mithun

Here is an example that will help you understand the concept of the maps in C++:

#include   
#include   
#include  
#include   
using namespace std;  
int main()  
{  
   map Emp;  
   Emp[001] = "Kanchan";  
   Emp[002] = "Manish";  
   Emp[003] = "Shipra";  
   Emp[004] = "Mithun";  
   cout << "Here is the Map size: " << Emp.size() << endl;  
   cout << endl << "Print Employees Details in Natural Order:" << endl;  
   for( map::iterator ii=Emp.begin(); ii!=Emp.end(); ++ii)  
   {  
       cout << (*ii).first << ": " << (*ii).second << endl;  
   }  
   cout << endl << "Print Employees details in Reverse Order:" << endl;  
   for( map::reverse_iterator ii=Emp.rbegin(); ii!=Emp.rend(); ++ii)  
   {  
       cout << (*ii).first << ": " << (*ii).second << endl;  
   }  
}
Output :
Here is the Map size: 4
Print Employees Details in Natural Order:
1: Kanchan
2: Manish
3: Shipra
Print Employees details in Reverse Order:
4: Mithun
3: Shipra
2: Manish
1: Kanchan