C++ Using find() method in a STL Multimap
In this tutorial, we will learn about the working of the erase() method in a Map in STL in the C++ programming language.
To understand the basic functionality of the Map Container in STL, we will recommend you to visit https://www.studytonight.com/cpp/stl/stl-container-map, where we have explained this concept in detail from scratch.
What is Multimap?
Multimap is similar to map with two additional functionalities:
Multiple elements can have the same or duplicate keys.
Multiple elements can have the same or duplicate key-value pair.
In Multimap, the
find(x) method returns an iterator to the very first element with key
For a better understanding of its implementation, refer to the well-commented C++ code given below.
using namespace std;
cout << "\n\nWelcome to Studytonight :-)\n\n\n";
cout << " ===== Program to demonstrate the concept find() method in Multimap, in CPP ===== \n\n\n";
cout << " In Multimap, find(x) returns an iterator to the very first elements with key x.\n\n";
//Multimap declaration (Multimap with key and value both as integers)
multimap<int, int> m;
//Filling the elements by using the insert() method.
cout << "Filling the Multimap with key-value pairs of integers in random order."; //Map automatically stores them in increasing order of keys
//make_pair() is used to insert a key value pair into the map
cout << "\n\nThe number of elements in the Multimap are: " << m.size();
cout << "\n\nThe elements of the Multimap m are: ";
multimap<int, int>::iterator i;
for (i = m.begin(); i != m.end(); i++)
cout << "( " << i->first << ", " << i->second << " ) ";
//Copying one multimap into another
multimap<int, int> m1(m.begin(), m.end());
multimap<int, int>::iterator f;
//Finding the very first element with key as 3
f = m1.find(3);
cout << "\n\nThe m1.find(3) method returns an iterator to the element: ( " << f->first << ", " << f->second << " ) ";
cout << "\n\n\n";
We hope that this post helped you develop a better understanding of the concept of the
find() method in the Map Container in STL and its implementation in C++. For any query, feel free to reach out to us via the comments section down below.
Keep Learning : )