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

set find() function in C++ STL

Last Updated : 16 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Special Case: When the element being searched for is not present in the set, an iterator pointing to the end of the set is returned.

Below program illustrates this:

C++




#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);
 
    // finding position of 6
    auto pos = s.find(6);
 
    // print element position if it is present in set
    if (pos != s.end())
        cout << "Element found at position : "
             << distance(s.begin(), pos) << endl;
    else
        cout << "Element not present in set";
 
    return 0;
}
 
// This code is modified by Susobhan Akhuli


Output

Element not present in set

Time complexity: O(log n) // n is the number of elements in the set

Auxiliary space: O(n)



Previous Article
Next Article

Similar Reads

bitset set() function in C++ STL
bitset::set() is a built-in STL in C++ which sets the bit to a given value at a particular index. If no parameter is passed, it sets all bits to 1. If only a single parameter is passed, it sets the bit at that particular index to 1. Syntax: set(int index, bool val) Parameter: The function accepts two parameters which are described below: index - th
2 min read
set value_comp() function in C++ STL
The set::value_comp() is an inbuilt function in cpp that returns a copy of the comparison object used by the container. This object determines the order of the elements in the container. It is a function pointer or a function object that takes two arguments of the same type as the container elements, and returns true if the first argument is consid
2 min read
set::lower_bound() function in C++ STL
The set::lower_bound() is a built-in function in C++ STL which returns an iterator pointing to the element in the container which is equivalent to k passed in the parameter. In case k is not present in the set container, the function returns an iterator pointing to the immediate next element which is just greater than k. If the key passed in the pa
3 min read
set::upper_bound() function in C++ STL
The set::upper_bound() is a built-in function in C++ STL which returns an iterator pointing to the immediate next element which is just greater than k. If the key passed in the parameter exceeds the maximum key in the container, then the iterator returned points to next of last element (which can be identified using set::end() function) in the set
3 min read
set equal_range() function in C++ STL
The set::equal_range() is a built-in function in C++ STL which returns an iterator of pairs. The pair refers to the range that includes all the elements in the container which have a key equivalent to k. Since set contains unique elements, the lower bound will be the element itself and the upper bound will point to the next element after key k. If
3 min read
set max_size() function in C++ STL
The set::max_size() is a built-in function in C++ STL which returns the maximum number of elements a set container can hold. Syntax: set_name.max_size() Parameters: This function does not accept any parameters. Return Value: This function returns the maximum number of elements a set container can hold. Below program illustrates the above function:
1 min read
set emplace_hint() function in C++ STL
The set::emplace_hint() is a built-in function in C++ STL which inserts a new element in the set. A position is passed in the parameter of the function which acts as a hint from where the searching operation starts before inserting the element at its current position. The position only helps the process to get faster, it does not decide where the n
2 min read
set count() function in C++ STL
The set::count() is a built-in function in C++ STL which returns the number of times an element occurs in the set. It can only return 1 or 0 as the set container contains unique elements only. Syntax The syntax of set::count is: set_name.count (element); ParametersThe function accepts one mandatory parameter element which specifies the element whos
2 min read
set crbegin() and crend() function in C++ STL
The set::crbegin() is a built-in function in C++ STL which returns a constant iterator pointing to the last element in the container. The iterator cannot be used to modify the elements in the set container. The iterators can be increased or decreased to traverse the set accordingly. Syntax: constant_iterator set_name.crbegin() Parameters: The funct
2 min read
set cbegin() and cend() function in C++ STL
The set::cbegin() is a built-in function in C++ STL which returns a constant iterator pointing to the first element in the container. The iterator cannot be used to modify the elements in the set container. The iterators can be increased or decreased to traverse the set accordingly. Syntax: constant_iterator set_name.cbegin() Parameters: The functi
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg