set find() Function in C++ STL Last Updated : 13 Feb, 2025 Comments Improve Suggest changes Like Article Like Report The std::set::find() is a built-in function in C++ STL that is used to find an element in the set container. It is a member function of std::set container so we can use it directly with any set object.Syntax set_name.find(key) Parameterskey: The element which we have to find.Return ValueIf the element is found, it returns an iterator to its position in the set.If the element is not found, then it returns the iterator to the end.Finding elements in a set is straightforward with the find() function. Example of set::find() C++ // C++ program to demonstrate the use of // set::find() function #include <bits/stdc++.h> using namespace std; int main() { // Create a set set<int> st; st.insert(11); st.insert(14); st.insert(2); st.insert(15); st.insert(3); // key1 find (exist in the set) int key1 = 3; // key2 find (does not exist in the set) int key2 = 1; // Check if key1 is found if (st.find(key1) != st.end()) cout << "Key '" << key1 << "' found" << endl; // Element not present else cout << "Key '" << key1 << "' not found!" << endl; // Check if key2 is found if (st.find(key2) != st.end()) cout << "Key '" << key2 << "' found" << endl; // key2 not found else cout << "Key '" << key2 << "' not found!" << endl; return 0; } OutputKey '3' found Key '1' not found! Complexity Analysis of set::find()Time Complexity: O(log n), where n is the number of elements.Space Complexity: O(1) Comment More infoAdvertise with us Next Article set::count() Function in C++ STL gopaldave Follow Improve Article Tags : Misc C++ STL CPP-Functions cpp-set +1 More Practice Tags : CPPMiscSTL Similar Reads Set in C++ STL In C++, sets are associative container which stores unique elements in some sorted order. By default, it is sorted ascending order of the keys, but this can be changed as per requirement. It provides fast insertion, deletion and search operations.Example: C++#include <iostream> #include <se 7 min read Different Ways to Initialize an Set in C++ Initializing a set means assigning some initial values to the elements of the set container. In this article, we will learn different methods to initialize an std::set in C++.Table of ContentUsing Initializer ListOne by One InitializationFrom Another std::setFrom Another STL Container or ArrayUsing 3 min read C++ STL Set Insertion and Deletion Prerequisite: Set A Set is a container implemented in C++ language in STL and has a concept similar to how the set is defined in mathematics. The fact that separates the set from the other containers is that it contains only the distinct elements and elements can be traversed in sorted order. Having 7 min read Different Ways to Insert Elements in Set in C++ STL Prerequisites: Set in C++ The C++ Standard Template Library offers containers called Sets. It functions essentially in the same ways as a binary search tree and is used to store different elements in increasing/decreasing order. There are different methods to insert elements in the set as mentioned 3 min read How to Access Elements in Set by Index in C++? In C++, elements of a set cannot be accessed directly by index or position. However, we can work around this limitation using iterators. In this article, we will learn how to access the elements in set by index in C++.The most efficient way to access a set element by index is to use the std::next() 3 min read Different ways to iterate over a set in C++ Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The values are stored in a specific order. Syntax: set<datatype> setname; Here,Datatype: Set can take any data type depending on the values, e.g. int, char, float, et 5 min read Commonly Used Methodsset::begin() and set::end() in C++ STLIn C++, std::set::begin() and std::set::end() are built-in functions used to retrieve set::iterators to the beginning and the end of the set container. Set uses bidirectional iterators, so the iterators returned by these functions support the dereferencing, increment, decrement, relational, and equa 3 min read set::size() in C++ STLIn C++, set::size() function is a built-in used to find the number of elements in the given set container. It is the member function of std::set class defined inside <set> header file. In this article, we will learn about the std::set::size() method in C++.Example:C++// C++ Program to illustra 2 min read set::empty() in C++ STLSets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::empty() empty() 2 min read set::insert() function in C++ STLThe std::set::insert() is a built-in function of C++ STL set container which is used to insert new elements in it. In this article, we will learn how to use set::insert() function in our C++ programs.SyntaxThe string::replace() function provides 6 different overloads for different purposes:st.insert 4 min read set::emplace() in C++ STLSets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::emplace() This f 4 min read set find() Function in C++ STLThe std::set::find() is a built-in function in C++ STL that is used to find an element in the set container. It is a member function of std::set container so we can use it directly with any set object.Syntax set_name.find(key) Parameterskey: The element which we have to find.Return ValueIf the eleme 2 min read set::count() Function in C++ STLThe std::set::count() is a built-in function in C++ STL which is used to count the number of times an element occurs in the set container. std::set container stores unique elements, so it can only return 1 or 0. Therefore, it is only used for checking if the element exists in the set or not.ExampleC 3 min read set::erase in C++ STLIn C++, the std::set::erase() is a built-in member function of std::set container that is used to remove the element(s) from the container. In this article, we will learn how we can use set::erase() function in our C++ program.The set::erase() can be used in different ways to erase element(s) from t 3 min read set::clear in C++ STLSets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::clear() clear() 2 min read set::swap() in C++ STLSets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::swap() This func 2 min read Other Member Methodsset max_size() function in C++ STLThe 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 ho 1 min read set emplace_hint() function in C++ STLThe 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 pro 2 min read set::rbegin() and set::rend() in C++ STLset::rbegin() is a built-in function in C++ STL which returns a reverse iterator pointing to the last element in the container. Syntax: reverse_iterator set_name.rbegin() Parameters: The function does not take any parameter. Return value: The function returns a reverse iterator pointing to the last 2 min read set crbegin() and crend() function in C++ STLThe 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 2 min read set cbegin() and cend() function in C++ STLThe 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 2 min read set::key_comp() in C++ STLset::key_comp() is an inbuilt function in C++ STL which returns a copy of the comparison object used by the container. By default, this is a less object, which returns the same as operator '. This object determines the order of the elements in the container. It is a function pointer or a function ob 2 min read set::lower_bound() Function in C++ STLThe std::set::lower_bound() method is used to find the first element in the set that is equal to or greater than the given value. It is a member function of std::set class and is defined inside <set> header file. In this article, we will learn about std::set::lower_bound() function in C++.Exam 3 min read Set upper_bound() in C++ STLIn C++, the set upper_bound() is a built-in method used to find the first element in the set that is just greater than the given value. In this article, we will learn about set upper_bound() function in C++.Letâs take a quick look at a simple illustration of the function:C++#include <bits/stdc++. 3 min read set equal_range() function in C++ STLThe 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 bou 3 min read set operator= in C++ STLThe â=â is an operator in C++ STL which copies (or moves) a set to another set and set::operator= is the corresponding operator function. There are three versions of this function: The first version takes reference of an set as an argument and copies it to an set. Syntax: ums1.operator=(set &set 2 min read set get_allocator() in C++ STLThe set::get_allocator() in C++ STL is an in-built function which returns the copy of the allocator object associated with the set. Syntax: mulset.get_allocator(); Parameters: This function does not accept any parameters. Return Value: This function returns the allocator associated with the set. Tim 2 min read Difference between std::set::upper_bound and std::upper_bound in C++ Prerequisites: Random-access Iterators, Bidirectional Iterators Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and 4 min read Difference between std::set vs std::vector in C++ STL Vectors: Vectors are containers similar to dynamic arrays, with the ability to resize when a new element is inserted or deleted from it. It is a template of Standard Template Library or STL, which provides more flexibility to the program. Elements of vectors are placed in contiguous storage and are 2 min read Like