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 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; } |
The set elements after 3 are: 3 4 5
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find Maximum and Minimum element in a Set in C++ STL
- Find the Deepest Node in a Binary Tree Using Queue STL - SET 2
- set::begin() and set::end() in C++ STL
- set::rbegin() and set::rend() in C++ STL
- Count number of unique Triangles using STL | Set 1 (Using set)
- unordered_multimap find() function in C++ STL
- map find() function in C++ STL
- multiset find() function in C++ STL
- unordered_set find() function in C++ STL
- unordered_multiset find() function in C++STL
- bitset set() function in C++ STL
- set value_comp() function in C++ STL
- set upper_bound() function in C++ STL
- set insert() function in C++ STL
- set equal_range() function in C++ STL
- set max_size() function in C++ STL
- set emplace_hint() function in C++ STL
- set count() function in C++ STL
- set crbegin() and crend() function in C++ STL
- set cbegin() and cend() function in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

