The Wayback Machine - https://web.archive.org/web/20250115165937/https://www.geeksforgeeks.org/unordered_set-find-function-in-c-stl/
Open In App

unordered_set find() function in C++ STL

Last Updated : 08 Nov, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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




// 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.

Time Complexity: O(1)

Auxiliary Space: O(n)

Program 2: 

CPP




// 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

Time Complexity: O(1)

Auxiliary Space: O(n)


Master C++ with our comprehensive C++ Programming Course Online, perfect for anyone from beginners to advanced learners. Learn everything from the fundamentals to advanced C++ concepts, and apply your knowledge through practical exercises.

Take the Three 90 Challenge! Finish 90% of the course in 90 days, and enjoy a 90% refund. Stay focused, track your progress, and challenge yourself to complete the course! Join today and begin your C++ programming journey!


Next Article

Similar Reads

three90RightbarBannerImg