Set upper_bound() in C++ STL Last Updated : 19 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In 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++.h> using namespace std; int main() { set<int> s = {1, 3, 5, 6, 7}; // Finding upper bound of 3 cout << *s.upper_bound(3); return 0; } Output5Explanation: In the above code, we found the upper bound 5 which is just greater than 3 in the given set s.This article covers the syntax, usage, and common examples of set upper_bound() method in C++:Table of ContentSyntax of set upper_bound()Examples of upper_bound()Find Upper Bound in Set of StringsFind Not Existing Upper BoundCheck if an Element Exists in the Setset upper_bound() in C++ – FAQsSyntax of set upper_bound()The set upper_bound() is the member method of std::set class defined inside <set> header file.s.upper_bound(k);Parametersk: Value whose upper bound is to be searched.Return ValueReturns an iterator to the first element that is just greater than the given value.If all the elements are less than or equal to the given value, returns iterator to the end.Examples of upper_bound()The following examples demonstrate the use of set upper_bound() function in different cases.Find Upper Bound in Set of Strings C++ #include <bits/stdc++.h> using namespace std; int main() { set<string> s = {"hello", "geeks", "welcome"}; // Finding upper bound of string "hello" cout << *s.upper_bound("hello"); return 0; } OutputwelcomeFind Not Existing Upper Bound C++ #include <bits/stdc++.h> using namespace std; int main() { set<int> s = {1, 5, 3, 7, 6}; // Finding upper bound of 7 in s auto it = s.upper_bound(7); if (it != s.end()) cout << *it; else cout << "Upper Bound Not Exists."; return 0; } OutputUpper Bound Not Exists.Explanation: In the above code, we are trying to find the upper bound of 7 and all the elements are less than or equal to the 7 in the set container. Hence the upper bound of 7 does not exist in the given set container.Check if an Element Exists in the Set C++ #include <bits/stdc++.h> using namespace std; int main() { set<int> s = {1, 5, 3, 7, 6}; // Finding upper bound of 7 in s auto it = --s.upper_bound(7); if (*it == 7) cout << "Exists."; else cout << "Not Exists."; return 0; } OutputExists.Explanation: As set is sorted, if the given value exists in the set, the set upper_bound() will return the iterator to the element next to the given element. This iterator can be decremented to point to the given element in the set. Comment More infoAdvertise with us Next Article set equal_range() function in C++ STL gopaldave Follow Improve Article Tags : Misc C++ STL CPP-Functions cpp-containers-library cpp-set +2 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