set::find() function in C++ STL

In C++, the standard library gives us the facility to use the Sets as a type of associative containers in which each element has to be unique. The set::find() is a C++ Standard Library function which is used to check whether the element is present in set or not. If element found it returns an iterator pointing to that element. If not found, it returns the position just after the last element in the set.

Syntax

Set_Name.find( Element );

Parameter: The set::find() function accepts only single parameter:

  • Element: This parameter is the element to be searched in the set container.

Return value: The set::find() function returns the iterator pointing to that element, otherwise the position just after the last element in the set

Example of set::find() Function

Program

#include <bits/stdc++.h> 
using namespace std; 
int main() 
{ 

    set<int> SET; 
  
    SET.insert(10); 
    SET.insert(4); 
    SET.insert(12); 
    SET.insert(15); 
  
    auto val = SET.find(4); 
  
    if (val != SET.end())
    {
        cout << "Element Found"; 
    }
    else
    {
        cout << "Element Not Found"; 
    }
  
    return 0; 
} 

Output

Element Found

This article is written by

Pratik Singhal

Pratik Singhal

Please comment below, if you have any doubts or find any error in the above article.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top