unordered_map find in C++ STL

find function in C++ is used to search for a specific key in an unordered map.

Syntax

unordered_map.find(key);

Parameters: It takes the key as a parameter.

Return values: If the given key exists in unordered_map it returns an iterator to that element otherwise it returns the end of the map iterator.

Below program illustrate the working of find function:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to demonstrate implementation of
// find function in unordered_map.
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    unordered_map<int, bool> um;
  
    um[12] = true;
    um[6789] = false;
    um[456] = true;
  
    // Searching for element 23
    if (um.find(23) == um.end())
        cout << "Element Not Present\n";
    else
        cout << "Element Present\n";
  
    // Searching for element 12
    if (um.find(12) == um.end())
        cout << "Element Not Present\n";
    else
        cout << "Element Present\n";
  
    return 0;
}

chevron_right


Output:

Element Not Present
Element Present

Time Complexity : O(1) on average.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.