Last Updated : 26 Sep, 2024
In C++, std::unordered_map::find function is used to search for a specific element using the key in an unordered map container. It is a member function of std::unordered_map container defined inside <unordered_map> header file.
SyntaxParametersum.find(key);
// C++ program to demonstrate how to use
// unordered_map::find() function
#include <bits/stdc++.h>
using namespace std;
void checkKey(unordered_map<int, string>& um, int key) {
// Searching for element with key
if (um.find(key) == um.end())
cout << "Key " << key <<
" Not Present\n";
else
cout << "Key " << key <<
" Present\n";
}
int main() {
unordered_map<int, string> um = {{12, "Geeks"},
{678, "Geeksfor"}, {88, "Gfg"}};
// Key1 and key2 which have to search
int key1 = 678;
int key2 = 456;
checkKey(um, key1);
checkKey(um, key2);
return 0;
}
Key 678 Present Key 456 Not Present
Time Complexity: O(1) on average. O(n) in worst case where n is the number of elements.
Space Complexity: O(1)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4