CodesJava

Easy learning with example program codes

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:

FUNCTIONUSES
get_allocatorTo return an allocator object to construct the Multimap.

 

Capacity:

FUNCTIONUSES
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:

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

 

Iterators:

FUNCTIONUSES
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:

FUNCTIONUSES
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:

FUNCTIONUSES
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:

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 <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:
error
Posted in CPP   








Copyright © 2019 CodesJava DMCA.com Protection Status