Multimap of pairs in C++ with Examples
Last Updated :
27 Dec, 2021
What is a multimap?
In C++, a multimap is an associative container that is used to store elements in a mapped fashion. Internally, a multimap is implemented as a red-black tree. Each element of a multimap is treated as a pair. The first value is referred to as key and the second value is referred to as value. Multimap is quite similar to a map but in the case of a multimap, we can have multiple same keys. Also, we cannot use square brackets ([]) to access the value mapped with a key. Like a map, keys of the multimap are sorted in ascending order by default.
Functions associated with multimap:
- begin(): Returns an iterator to the first element in the multimap
- end(): Returns an iterator to the theoretical element that follows the last element in the multimap
- size(): Returns the number of elements in the multimap
- max_size(): Returns the maximum number of elements that the multimap can hold
- empty(): Returns whether the multimap is empty
- insert(key, value): Adds a new element or pair to the multimap
- erase(iterator position): Removes the element at the position pointed by the iterator
- erase(const x): Removes the key-value ‘x’ from the multimap
- clear(): Removes all the elements from the multimap
What is a pair?
Utility header in C++ provides us pair container. A pair consists of two data elements or objects.
- The first element is referenced as ‘first’ and the second element as ‘second’ and the order is fixed (first, second).
- Pair is used to combine together two values that may be different in type. Pair provides a way to store two heterogeneous objects as a single unit.
- Pair can be assigned, copied, and compared. The array of objects allocated in a map or hash_map is of type ‘pair’ by default in which all the ‘first’ elements are unique keys associated with their ‘second’ value objects.
To access the elements, we use variable name followed by dot operator followed by the keyword first or second.
How to access a pair?
The elements of a pair can be accessed by using the dot (.) operator.
Syntax:
auto fistElement = myPair.first;
auto fistElement = myPair.second;
Multimap of pairs
A multimap of pairs is a multimap in which either of the key or value is a pair itself. Two pairs are considered to be equal if the corresponding first and second elements of pairs are equal. Now if there is a need to store more than one copy of a pair along with other elements that too in a sorted or particular order, in such cases multiset of pairs comes in handy.
Syntax:
multimap<pair<dataType1, dataType2>> myMultimap;
Here,
dataType1 and dataType2 can be similar or dissimilar data types.
Example 1: Below is the C++ program to demonstrate the working of a multimap of pairs.
C++
#include <bits/stdc++.h>
using namespace std;
void print(multimap<pair< int , int >,
bool >& myContainer)
{
cout << "Key(pair of integers)" <<
" " <<
"Value(boolean)\n\n" ;
for ( auto pr : myContainer)
{
pair< int , int > myPair = pr.first;
cout << '[' << myPair.first << " , " <<
myPair.second << ']' <<
" " <<
pr.second << '\n' ;
}
}
int main()
{
multimap<pair< int , int >, bool > myContainer;
pair< int , int > pair1;
pair1 = make_pair(100, 200);
pair< int , int > pair2;
pair2 = make_pair(200, 300);
pair< int , int > pair3;
pair3 = make_pair(300, 400);
pair< int , int > pair4;
pair4 = make_pair(100, 200);
myContainer.insert(pair<pair< int , int >,
bool >(pair1, true ));
myContainer.insert(pair<pair< int , int >,
bool >(pair2, false ));
myContainer.insert(pair<pair< int , int >,
bool >(pair3, true ));
myContainer.insert(pair<pair< int , int >,
bool >(pair4, false ));
print(myContainer);
return 0;
}
|
Output
Key(pair of integers) Value(boolean)
[100, 200] 1
[100, 200] 0
[200, 300] 0
[300, 400] 1
Example 2: Below is the C++ program to demonstrate the working of a multimap of pairs.
C++
#include <bits/stdc++.h>
using namespace std;
void print(multimap<pair<string, int >,
bool >& myContainer)
{
cout << "Key(pair of integers)" <<
" " <<
"Value(boolean)\n\n" ;
for ( auto pr : myContainer)
{
pair<string, int > myPair = pr.first;
cout << '[' << myPair.first <<
" , " << myPair.second <<
']' << " " <<
" " <<
pr.second << '\n' ;
}
}
int main()
{
multimap<pair<string, int >, bool > myContainer;
pair<string, int > pair1;
pair1 = make_pair( "GFG" , 100);
pair<string, int > pair2;
pair2 = make_pair( "C++" , 200);
pair<string, int > pair3;
pair3 = make_pair( "CSS" , 300);
pair<string, int > pair4;
pair4 = make_pair( "GFG" , 400);
myContainer.insert(pair<pair<string, int >,
bool >(pair1, true ));
myContainer.insert(pair<pair<string, int >,
bool >(pair2, false ));
myContainer.insert(pair<pair<string, int >,
bool >(pair3, true ));
myContainer.insert(pair<pair<string, int >,
bool >(pair4, false ));
print(myContainer);
return 0;
}
|
Output
Key(pair of integers) Value(boolean)
[C++, 200] 0
[CSS, 300] 1
[GFG, 100] 1
[GFG, 400] 0
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...