Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

unordered_multimap find() function in C++ STL

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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




// 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;
}

Output:

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

Program 2: 

CPP




// 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 or 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;
}

Output:

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

My Personal Notes arrow_drop_up
Last Updated : 23 Feb, 2023
Like Article
Save Article
Similar Reads