C++ Unordered_map Library - find() Function


The C++ function std::unordered_map::find() finds an element associated with key k.

If operation succeeds then methods returns iterator pointing to the element otherwise it returns an iterator pointing the map::end().


Following is the declaration for std::unordered_map::find() function form std::unordered_map header.


iterator find (const key_type& k);
const_iterator find (const key_type& k) const;


k − Key to be searched.

Return value

If object is constant qualified then method returns a constant iterator otherwise non-constant iterator.

Time complexity

Constant i.e. O(1) in average case.

Linear i.e. O(n) in worst case.


The following example shows the usage of std::unordered_map::find() function.

#include <iostream>
#include <unordered_map>

using namespace std;

int main(void) {
   unordered_map<char, int> um = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5}

   auto it = um.find('d');

   cout << "Iterator points to " << it->first
       << " = " << it->second << endl;

   return 0;

Let us compile and run the above program, this will produce the following result −

Iterator points to d = 4