C++ Standard Library C++ STL Library

C++ set - find() Function

The C++ set::find function is used to search the container for an element equivalent to the specified value and returns the iterator to it if found, else returns the iterator to set::end.


iterator find (const value_type& val) const;
const_iterator find (const value_type& val) const;
iterator find (const value_type& val);


val Specify value to search for.

Return Value

An iterator to the element if val is found, or set::end if val is not found.

Time Complexity

Logarithmic i.e, Θ(log(n)).


In the example below, the set::find function is used to find the element in the set called MySet.

#include <iostream>
#include <set>
using namespace std;
int main (){
  set<int> MySet{55, 25, 128, 5, 72};
  set<int>::iterator it;

  it = MySet.find(25);

  cout<<"MySet contains: ";
  for(it = MySet.begin(); it != MySet.end(); ++it)
    cout<<*it<<" ";

  return 0;

The output of the above code will be:

MySet contains: 55 72 128 

❮ C++ <set> Library