Skip to content
Related Articles

Related Articles

unordered_map find in C++ STL
  • Last Updated : 24 Dec, 2018

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:




// 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;
}
Output:
Element Not Present
Element Present

Time Complexity : O(1) on average.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :