unordered_multimap find() function in C++ STL

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_multimap::find() function
#include <iostream>
#include <unordered_map>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap<int, int> sample;
  
    // inserts key and element
    sample.insert({ 1, 2 });
    sample.insert({ 1, 2 });
    sample.insert({ 2, 3 });
    sample.insert({ 3, 4 });
    sample.insert({ 2, 6 });
  
    // find the element with key 1 and print
    auto it = sample.find(1);
    if (it != sample.end())
        cout << 1 << ":" << it->second << endl;
    else
        cout << "element with key 1 not found\n";
  
    // find the element with
    // key 2 and print
    it = sample.find(2);
    if (it != sample.end())
        cout << 2 << ":" << it->second << endl;
    else
        cout << "element with key 2 not found\n";
  
    // find the element with
    // key 100 and print
    it = sample.find(100);
    if (it != sample.end())
        cout << 100 << ":" << it->second << endl;
    else
        cout << "element with key 100 not found\n";
    return 0;
}

chevron_right


Output:

1:2
2:6
element with key 100 not found

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_multimap::find()
#include <iostream>
#include <unordered_map>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap<char, char> sample;
  
    // inserts element
    sample.insert({ 'a', 'b' });
    sample.insert({ 'a', 'b' });
    sample.insert({ 'a', 'd' });
    sample.insert({ 'b', 'e' });
    sample.insert({ 'b', 'd' });
  
    // find the element with
    // key r and print
    auto it = sample.find('r');
    if (it != sample.end())
        cout << "r"
             << ":" << it->second << endl;
    else
        cout << "element with key r not found\n";
  
    // find the element with
    // key a and print
    it = sample.find('a');
    if (it != sample.end())
        cout << 'a' << ":" << it->second << endl;
    else
        cout << "element with key a not found\n";
  
    // find the element with
    // key 'b' and print
    it = sample.find('b');
    if (it != sample.end())
        cout << "b"
             << ":" << it->second << endl;
    else
        cout << "element with key b not found\n";
  
    return 0;
}

chevron_right


Output:

element with key r not found
a:d
b:d


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.