C++ set upper_bound()
C++ set upper_bound() function is used to return an iterator pointing to the value in the set container which is larger to val passed in the parameter.
Syntax
Parameter
val: value to be searched in the set container.
Return value
It returns an iterator pointing to the value in the set container which is larger to val passed in the parameter. If there is no such element return end().
Complexity
Logarithmic in size.
Iterator validity
No changes.
Data Races
The container is accessed (neither the const nor the non-const versions modify the container).
Concurrently accessing the elements of a set is safe.
Exception
If an exception is thrown, there are no changes in the container.
Example 1
Let’s see the simple example to get the upper bound of given value:
Output:
Upper bound of b is(>): c
In the above example, when we try to find the upper bound of element b then it will return greater element of b i.e. c
Example 2
Let’s see a simple example to erase the elements of set from lower bound to upper bound:
Output:
myset contains: 10 20 70 80 90
In the above example, erase() function erased the element of set from lower bound(=) to upper bound(>) and print the remaining content.
Example 3
Let’s see a simple example:
Output:
The upper bound of key 11 is 12 The upper bound of key 13 is 14 The upper bound of key 17 is 4
In the above example, when we try to find the upper bound of a value which is not present in the set container but does not exceeds the maximum value then it will return greater value i.e. when we trying to find upper bound of 13 then it will return 14 and when we trying to find upper bound of a value which is not present in the set and exceeds the maximum value of container then it will return to the end().
Example 4
Let’s see a simple example:
Output:
The first element of set s1 with a key greater than 20 is: 30. The set s1 doesn't have an element with a key greater than 30. The first element of s1 with a key greater than that of the initial element of s1 is: 20.