The pair is a simple container defined in <utility> class having two elements or objects. The first element is always referenced as “first” and the second element is always referenced as “second”. The order is fixed i.e, (first, second), it is used to combine two heterogeneous values.
#include<iostream>
#include<utility>
using namespace std;
int main() {
// declaring a pair of int and char
pair PAIR1;
PAIR1.first = 100; // 1st element of pair
PAIR1.second = ‘G’; // 2nd element of pair
cout << PAIR1.first << ””; // 100
cout << PAIR1.second << endl; //G
return 0;
}
You can also try this code with Online C++ Compiler
// CPP program to illustrate the
// erase() and count() function
// in multiset
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
int main() {
// Create a multiset
multiset s = {
15,
10,
15,
11,
10,
18,
18,
20,
20
};
// PRINT THE MULTISET
cout << “The multiset elements are: “;
for (auto it = s.begin(); it != s.end(); it++)
cout << * it << ”“;
return 0;
}
You can also try this code with Online C++ Compiler
The multimap container in C++ is similar to the map container with an addition that a multimap can have multiple key-value pairs with the same key. Rather than each element is unique, the key-value and mapped value pair have to be unique in this case. Multimap is also implemented using Red-Black trees and hence the basic operations like search, insert, delete works in O(log N) time for multimap as well.
SYNTAX: multimap variable_name;
IMPLEMENTATION:
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
int main() {
// CREATE A MULTIMAP
multimap mp;
//INSERT INTO MULTIMAP
mp.insert({10,20});
mp.insert({5,50});
mp.insert({10,25});
// PRINT THE MULTIMAP
for (auto x: mp)
cout << x.first << ”“ << x.second << endl;
return 0;
}
You can also try this code with Online C++ Compiler
The default behaviour of both of these data structures multiset and multimap is to store elements in ascending order. When a pair of a multiset is created then by default, it will sort all the pairs in increasing order according to the first element of all the pairs and if the first element of any two or more than two pairs are equal then it will sort the pair according to the second element of the pair.
When a pair of multimap is created then by default, it will sort all the pairs in increasing order according to the first element of all the pairs and if the first element of any two or more than two pair are equal then it will print the pair according to the order of insertion to the pair of multimap.
Below are the programs to illustrate the difference:
Program 1: Pair in multi-set
// C++ program print the data of
// multiset by inserting using pair
#include<bits/stdc++.h>
using namespace std;
// Function to print the data stored
// in pair of multiset
void printData(multiset > g) {
// Declare iterator
multiset > ::iterator i;
// Iterate through pair of multiset
for (i = g.begin(); i != g.end(); ++i) {
// Print the pairs
cout << i -> first << ”” << i -> second << endl;
}
}
int main() { // Declare pair of multiset multiset > g;
// Insert Data
g.insert(make_pair(1, “yukti”));
g.insert(make_pair(2, “umang”));
g.insert(make_pair(3, “vinay”));
g.insert(make_pair(3, “vijay”));
g.insert(make_pair(4, “kanak”));
// Function call to print the data
printData(g);
return 0;
}
You can also try this code with Online C++ Compiler
Explanation: In the above program, we have created pairs of integer and string in which names are paired with each integer and are inserted in the multi-set. According to the default behaviour of multi-set the data is arranged in ascending order according to the first element but when the first element is the same it will arrange those elements according to the second value.
For the pair (3, “Vijay”) and (3, “Vinay”) the first element in the pair i.e., 3 is same for both “Vijay” and “Vinay” so it will arrange the pairs according to second element “Vijay” then “Vinay” (in alphabetical sequence).
Program 2: Pair in multi-map
// C++ program print the data of
// multimap by inserting using pair
#include<bits/stdc++.h>
using namespace std;
// Function to print the data stored
// in pair of multimap
void printData(multimap g) {
// Declare iterator
multimap::iterator i;
// Iterate through pair of multiset
for (i = g.begin(); i != g.end(); ++i) {
// Print the pairs
cout << i -> first << ”” << i -> second << endl;
}
}
int main() {
// Declare pair of multimap
multimap g;
// Insert data
g.insert(make_pair(1, “yukti”));
g.insert(make_pair(2, “umang”));
g.insert(make_pair(3, “vinay”));
g.insert(make_pair(3, “vijay”));
g.insert(make_pair(4, “kanak”));
// Function call to print the data
printData(g);
return 0;
}
You can also try this code with Online C++ Compiler
Explanation of the above Code: In the above program, we have again inserted the same pairs but this time in multi-map. According to the default behaviour of multi-map the data is arranged in ascending order according to key but when the key is same unlike the multi-set it will see the precedence which element is inserted first and then it will arrange according to that sequence.
So, as in the output shown we can see that as the key 3 is same for “vinay” and “vijay” so it will follow the sequence in which the pairs were inserted in the multi-map, that’s why “vinay” came first before “vijay” in output.
Tabular difference
PAIR IN MULTISET
PAIR IN MULTIMAP
In pair of multiset pair is used to mapped key with specific value.
Default behaviour is to insert element as a key-value pair.
When a pair of a multiset is created then by default, it will sort all the pairs in increasing order according to the first element of all the pairs and if the first element of any two or more than two pairs are equal then it will sort the pair according to the second element of the pair.
When a pair of a multimap is created then by default, it will sort all the pairs in increasing order according to the first element of all the pairs and if the first element of any two or more than two pair are equal then it will print the pair according to the order of insertion to the pair of multimap.
What is the difference between map and multimap in Java?
Both the map and the multimap in java are containers for key/value pairs that are managed as single components. The primary distinction between the two is that keys in a map must be unique, whereas keys in a multimap in java may be duplicated.
How do I remove a multimap key in Java?
The remove() method in Guava's MultiMap in java removes a single key-value pair from the multimap that matches the specified key-value pair. If the pair is removed, it returns true; otherwise, it returns false if no such pair is found.
How do I get my multimap key?
We can use Multimap's member function equal range to find all values of a key (). It takes as an argument the key and returns a pair of multimap iterators. This returned pair contains a range containing the entries with the given key.
What happens when HashMap duplicates keys?
HashMap stores key-value pairs and prevent duplicate keys. If the key is duplicated, the old value replaces the old key.
Conclusion
In this article, we learned about Pairs in Multiset and Multimap in C++.
Want To Learn More About brand-new technologies? We Have A Whole Category, visit Coding Ninjas or explore yourself in the following links: