unordered_multimap find() function in C++ STL
Last Updated :
23 Feb, 2023
The unordered_multimap::find() is a built-in function in C++ STL which returns an iterator which points to one of the elements which has the key k. If the container does not contain any element with key k, then it returns an iterator which points to the position which is past the last element in the container.
Syntax:
unordered_multimap_name.find(k)
Parameters: The function accepts a mandatory parameter k which specifies the key.
Return Value: It returns an iterator which points to the position where an element with key k is.
Below programs illustrate the above function:
Program 1:
CPP
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
unordered_multimap< int , int > sample;
sample.insert({ 1, 2 });
sample.insert({ 1, 2 });
sample.insert({ 2, 3 });
sample.insert({ 3, 4 });
sample.insert({ 2, 6 });
auto it = sample.find(1);
if (it != sample.end())
cout << 1 << ":" << it->second << endl;
else
cout << "element with key 1 not found\n";
it = sample.find(2);
if (it != sample.end())
cout << 2 << ":" << it->second << endl;
else
cout << "element with key 2 not found\n";
it = sample.find(100);
if (it != sample.end())
cout << 100 << ":" << it->second << endl;
else
cout << "element with key 100 not found\n";
return 0;
}
|
Output:
1:2
2:6
element with key 100 not found
Program 2:
CPP
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
unordered_multimap< char , char > sample;
sample.insert({ 'a' , 'b' });
sample.insert({ 'a' , 'b' });
sample.insert({ 'a' , 'd' });
sample.insert({ 'b' , 'e' });
sample.insert({ 'b' , 'd' });
auto it = sample.find( 'r' );
if (it != sample.end())
cout << "r"
<< ":" << it->second << endl;
else
cout << "element with key or not found\n";
it = sample.find( 'a' );
if (it != sample.end())
cout << 'a' << ":" << it->second << endl;
else
cout << "element with key a not found\n";
it = sample.find( 'b' );
if (it != sample.end())
cout << "b"
<< ":" << it->second << endl;
else
cout << "element with key b not found\n";
return 0;
}
|
Output:
element with key r not found
a:d
b:d
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...