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.

Syntax

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

Parameters

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

Example:

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);
  MySet.erase(it);
  MySet.erase(MySet.find(5));

  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

5