Skip to content
Related Articles

Related Articles

set find() function in C++ STL
  • Difficulty Level : Basic
  • Last Updated : 27 Jan, 2021

The set::find is a built-in function in C++ STL which returns an iterator to the element which is searched in the set container. If the element is not found, then the iterator points to the position just after the last element in the set. 
Syntax: 
 

 
set_name.find(element) 

Parameters: The function accepts one mandatory parameter element which specifies the element to be searched in the set container. 
Return Value: The function returns an iterator which points to the element which is searched in the set container. If the element is not found, then the iterator points to the position just after the last element in the set.
Below program illustrates the above function. 
 

CPP




// CPP program to demonstrate the
// set::find() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
 
    // Initialize set
    set<int> s;
 
    s.insert(1);
    s.insert(4);
    s.insert(2);
    s.insert(5);
    s.insert(3);
 
    // iterator pointing to
    // position where 2 is
    auto pos = s.find(3);
 
    // prints the set elements
    cout << "The set elements after 3 are: ";
    for (auto it = pos; it != s.end(); it++)
        cout << *it << " ";
 
    return 0;
}
Output: 
The set elements after 3 are: 3 4 5

 

Time Complexity: The time complexity of set_name.find( key ) is O( log N ). As the elements are stored in a sorted manner by default.

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 :