The unordered_set::find() function is a built-in function in C++ STL which is used to search for an element in the container. It returns an iterator to the element, if found else, it returns an iterator pointing to unordered_set::end().
Syntax :
unordered_set_name.find(key)
Parameter: This function accepts a mandatory parameter key which specifies the element to be searched for.
Return Value: It returns an iterator to the element if found, else returns an iterator pointing to the end of unordered_set.
Below programs illustrate the unordered_set::find() function:
Program 1:
CPP
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set<string> sampleSet = { "geeks1", "for", "geeks2" };
if (sampleSet.find("geeks1") != sampleSet.end()) {
cout << "element found." << endl;
}
else {
cout << "element not found" << endl;
}
return 0;
}
|
Time Complexity: O(1)
Auxiliary Space: O(n)
Program 2:
CPP
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set<string> sampleSet = { "geeks1", "for", "geeks2" };
if (sampleSet.find("geeksforgeeks") != sampleSet.end()) {
cout << "found" << endl;
}
else {
cout << "Not found" << endl;
}
return 0;
}
|
Time Complexity: O(1)
Auxiliary Space: O(n)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
08 Nov, 2023
Like Article
Save Article