![]() |
VOOZH | about |
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:
5
Explanation: 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 Content
The set upper_bound() is the member method of std::set class defined inside <set> header file.
s.upper_bound(k);
The following examples demonstrate the use of set upper_bound() function in different cases.
welcome
Upper 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.
Exists.
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.