CPP Multimap

Multimaps in CPP are used to store sorted key-value pair. They are the associative containers. Each key in a Multimap is not necessarily unique. Unlike maps, duplication of keys is allowed in case of multimap. CPP facilitates insertion and deletion of a key in a Multimap but do not allow any modifications, however, values can be modified.

Member Functions of a CPP Multimap:

Allocator:

FUNCTION USES
get_allocator To return an allocator object to construct the Multimap.

 

Capacity:

FUNCTION USES
empty() To determine whether the Multimap is empty or not.
max_size() To determine the maximum size of the Multimap.
size() To determine number of elements in the Multimap.

 

Constructor/Destructor:

FUNCTION USES
(constructor) To construct a Multimap.
(destructor) To destruct a Multimap.
operator= To copy the elements of the Multimap to another Multimap.

 

Iterators:

FUNCTION USES
begin() To point to the first element of the Multimap.
cbegin() To return a constant iterator to the beginning of the Multimap.
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:

FUNCTION USES
clear() To remove all the elements of the Multimap.
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 Multimap..
swap() To exchange the contents of two Multimaps.

 

Non-Member Overloaded Functions:

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

 

Observers:

FUNCTION USES
key_comp To return a copy of key comparison object.
value_comp To return a copy of value comparison object.

 

Operations:

FUNCTION USES
count To get the number of elements matching with given key.
equal_range To get the range of elements matches with given key.
find To find an element with given key.
lower_bound To get an iterator to lower bound.
upper_bound To get an iterator to upper bound.

 

Example:

#include <iostream>  
#include <map>  
#include <string>  
 
using namespace std;  
 
int main()  
{  
multimap<int, string> fruits = {  
{100,"APPLE"},  
{200, "MANGO"},  
{200, "BANANA"},  
{300, "GUAVA"}  
};  
 
cout << "Size of map fruits: " << fruits.size() <<endl; 	 
cout << "Elements in fruits: " << endl;  
 
for (multimap<int, string>::iterator i = fruits.begin(); i != fruits.end(); ++i)  
{  
cout << "  [" << (*i).first << ", " << (*i).second << "]" << endl;  
}  
 
return 0;  
}

Output

Size of map fruits: 4           
Elements in fruits: 
  [100, APPLE]        
  [200, MANGO]           
  [200, BANANA]      
  [300, GUAVA]
Please follow and like us:
Content Protection by DMCA.com