C++ set upper_bound() Function
Example
Find the first number that is greater than 5:
set<int> numbers = {1, 7, 3, 2, 5, 9};
auto it = numbers.upper_bound(5);
cout << *it;
Definition and Usage
The upper_bound() function returns an iterator (a pointer-like object that points to an element) to the first element that is greater than the given value. An element that is equal to the value is not included.
The elements in a set are sorted, so in the example, the result is 7, the first number after 5. If there is no such element, it returns end().
The upper_bound function is defined in the <set> header file.
Tip: Use lower_bound() to also include an element that is equal to the value.
Syntax
set.upper_bound(type value)
type is the type of the elements in the set, like int in the example.
Parameter Values
| Parameter | Description |
|---|---|
| value | Required. The value to compare with. |
Technical Details
| Returns: | An iterator pointing to the first element that is greater than value. If there is no such element, it returns set.end(). |
|---|
Related Pages
Read more about sets in our Sets Tutorial.