As we all know, the C++ programming language has many in-built functions to help us avoid writing long lines of code. One such function is the multimap find function available in the rich library of C++ programming language, the Standard Template Library(STL). It will help us return an iterator which refers to or points to the position of the key containing the data traversal in the long or small multimap we have created. Suppose you have a different objective of knowing all the fundamental data values. In that case, you can refer to the equal range function available for us in the rich library of C++ programming language, the Standard Template Library(STL).
Multimap in C++
Example
// Here we are writing down the C++ programming language code to
// demonstrate
// the concept of multimap find() in C++ STL(Standard Template Library)
#include <iostream>
#include <iterator>
#include <map>
using namespace std;
// The main driver code functionality starts from here!
int main()
{
// Here, we are trying to create an initialiser container
// which will help us with creating a multimap using C++ STL
multimap<int, int> gquiz1;
// these insert functions which we have written below help us
// with creating a multimap holding the values inserted below!
gquiz1.insert(pair<int, int>(1, 40));
gquiz1.insert(pair<int, int>(2, 30));
gquiz1.insert(pair<int, int>(3, 60));
gquiz1.insert(pair<int, int>(6, 50));
gquiz1.insert(pair<int, int>(6, 10));
// elements which we are going to display on the Output screen are
multimap<int, int>::iterator itr;
cout << "\nThe multimap gquiz1 is : \n";
cout << "\tKEY\tELEMENT\n";
// here we are writing the for loop, which contains the auto code
// functionality containing the find() function at different positions
for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
cout << endl;
// here, we are trying to add elements randomly,
// as it helps us to check the sorted keys property in C++ STL
gquiz1.insert(pair<int, int>(4, 50));
gquiz1.insert(pair<int, int>(5, 10));
// the below code helps us with printing multimap gquiz1 again
cout << "\nmultimap quiz1 after"
<< " adding the extra elements is : \n";
cout << "\tkey\telement\n";
for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
cout << endl;
// the below code snippet below assigning the elements from quiz1 to quiz2
multimap<int, int> gquiz2(gquiz1.begin(),
gquiz1.end());
// the below code snippet prints all the elements of the multimap gquiz2
cout << "\nThe multimap gquiz2 after"
<< " assign from gquiz1 is : \n";
cout << "\tKEY\tELEMENT\n";
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
cout << endl;
// here, we are trying to remove all elements up to
// key with the value 3 in quiz2
cout << "\n the quiz2 after the removal of"
<< " elements less than key = 3 : \n";
cout << "\t key \t element \n";
gquiz2.erase(gquiz2.begin(), gquiz2.find(3));
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
// the below code helps us with removing all the elements with key = 4
int num;
num = gquiz2.erase(4);
cout << "\n quiz 2.erase(4) : ";
cout << num << " removed \n";
cout << "\tkey\telement\n";
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
// new line printing statement the endl statement
cout << endl;
// the lower and upper bounds of the much awaited
// for multimap gquiz1 key = 5
cout << "the quiz1.lower bound (5) : "
<< "\tkey = ";
cout << gquiz1.lower_bound(5)->first << '\t';
cout << "\telement = "
<< gquiz1.lower_bound(5)->second
<< endl;
cout << "the quiz1.upper bound (5): "
<< "\tKEY = ";
cout << gquiz1.upper_bound(5)->first << '\t';
cout << "\telement = "
<< gquiz1.upper_bound(5)->second
<< endl;
return 0;
// The main driver code functionality ends from here!
}
Output:
Output
The multimap gquiz1 is :
KEY ELEMENT
1 40
2 30
3 60
6 50
6 10
Multimap quiz1 after adding the extra elements is :
key element
1 40
2 30
3 60
4 50
5 10
6 50
6 10
The multimap gquiz2 after assignment from gquiz1 is :
KEY ELEMENT
1 40
2 30
3 60
4 50
5 10
6 50
6 10
quiz2 after the removal of elements less than key = 3 :
key element
3 60
4 50
5 10
6 50
6 10
quiz 2.erase(4) : 1 removed
key element
3 60
5 10
6 50
6 10
the quiz1.lower bound (5) : key = 5 element = 10
the quiz1.upper bound (5) : KEY = 6 element = 50
Multimap find in C++ STL
Syntax
Example
iterator multimap_name.find(key)
constant iterator multimap_name.find(key)
C++ code
Example
// Here we are writing down the C++ programming language code to demonstrate
// the concept of multimap find() in C++ STL(Standard Template Library)
#include <bits/stdc++.h>
using namespace std;
// The main driver code functionality starts from here!
int main()
{
// Here, we are trying to create an initialiser container
// which will help us with creating a multimap using C++ STL
multimap<int, int> mp;
// these insert functions which we have written below help us
// with creating a multimap holding the values inserted below!
mp.insert({ 12, 130 });
mp.insert({ 11, 140 });
mp.insert({ 12, 160 });
mp.insert({ 31, 210 });
mp.insert({ 11, 501 });
mp.insert({ 14, 510 });
// elements which we are going to display on the Output screen are
cout << "The elements from position 3 in multimap are : \n";
cout << "Key\telements : \n";
// here we are writing the for loop, which contains the auto code
// functionality containing the find() function at position 3
for (auto itr = mp.find(31); itr != mp.end(); itr++)
cout << itr->first
<< '\t' << itr->second << '\n';
return 0;
// The main driver code functionality ends from here!
}
Output:
Output
The elements from position 3 in multimap are :
Key elements :
31 210