In this shot, we will learn how to use the unordered_map::find()
function.
The unordered_map::find()
function is available in the <unordered_map>
header file in C++.
The unordered_map::find()
searches a specific key in the unordered map.
The syntax of the unordered_map::find()
function is given below:
Iterator find(K key);
The unordered_map::find()
method accepts the parameter mentioned below:
The unordered_map::find()
returns one of the two values:
Let us have a look at the code now:
#include <iostream>#include <unordered_map>using namespace std;int main(){unordered_map<int,int> umap ={{12, 32},{16, 899},{89, 90},{66, 46}};if(umap.find(51) == umap.end())cout<<"The element with key = 51 is not present." << endl;elsecout<<"The element with key = 51 is present."<<endl;if(umap.find(89) == umap.end())cout<<"The element with key = 89 is not present." << endl;elsecout<<"The element with key = 89 is present." << endl;return 0;}
main()
function.if-else
statement to check whether the element with the specified key is present or not using the unordered_map::find()
function. Here, we observe that the key is not present, and the corresponding message is printed.if-else
statement again to check whether the element with the specified key is present or not using the unordered_map::find()
function. This time, the key is present in the unordered map, and the corresponding message is printed.This is how we use the unordered_map::find()
function in C++.