Skip to content
Related Articles

Related Articles

unordered_set find() function in C++ STL
  • Difficulty Level : Medium
  • Last Updated : 28 Sep, 2018

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:




// C++ program to illustrate the
// unordered_set::find() function
  
#include <iostream>
#include <string>
#include <unordered_set>
  
using namespace std;
  
int main()
{
  
    unordered_set<string> sampleSet = { "geeks1", "for", "geeks2" };
  
    // use of find() function
    if (sampleSet.find("geeks1") != sampleSet.end()) {
        cout << "element found." << endl;
    }
    else {
        cout << "element not found" << endl;
    }
  
    return 0;
}
Output:
element found.

Program 2:




// CPP program to illustrate the
// unordered_set::find() function
  
#include <iostream>
#include <string>
#include <unordered_set>
  
using namespace std;
  
int main()
{
  
    unordered_set<string> sampleSet = { "geeks1", "for", "geeks2" };
  
    // use of find() function
    if (sampleSet.find("geeksforgeeks") != sampleSet.end()) {
        cout << "found" << endl;
    }
    else {
        cout << "Not found" << endl;
    }
  
    return 0;
}
Output:
Not found
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 :