CARVIEW |
Select Language
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Sun, 27 Jul 2025 08:34:59 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Location: /reference/algorithm/upper_bound/
HTTP/1.1 200 OK
Server: nginx
Date: Sun, 27 Jul 2025 08:34:59 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
ETag: W/"ab2b-N5CNsG46zLgC3pyZQNpLG+XYXfU"
Content-Encoding: gzip
Returns an iterator pointing to the first element in the range
The elements are compared using
The function optimizes the number of comparisons performed by comparing non-consecutive elements of the sorted range, which is specially efficient for random-access iterators.
Unlike lower_bound, the value pointed by the iterator returned by this function cannot be equivalent to val, only greater.
The behavior of this function template is equivalent to:
If no element in the range compares greater than val, the function returns last.
Output:
On non-random-access iterators, the iterator advances produce themselves an additional linear complexity in N on average.
Note that invalid arguments cause undefined behavior.
Reference
C library:
- <cassert> (assert.h)
- <cctype> (ctype.h)
- <cerrno> (errno.h)
-
<cfenv> (fenv.h)C++11
- <cfloat> (float.h)
-
<cinttypes> (inttypes.h)C++11
- <ciso646> (iso646.h)
- <climits> (limits.h)
- <clocale> (locale.h)
- <cmath> (math.h)
- <csetjmp> (setjmp.h)
- <csignal> (signal.h)
- <cstdarg> (stdarg.h)
-
<cstdbool> (stdbool.h)C++11
- <cstddef> (stddef.h)
-
<cstdint> (stdint.h)C++11
- <cstdio> (stdio.h)
- <cstdlib> (stdlib.h)
- <cstring> (string.h)
-
<ctgmath> (tgmath.h)C++11
- <ctime> (time.h)
-
<cuchar> (uchar.h)C++11
- <cwchar> (wchar.h)
- <cwctype> (wctype.h)
Containers:
-
<array>C++11
- <deque>
-
<forward_list>C++11
- <list>
- <map>
- <queue>
- <set>
- <stack>
-
<unordered_map>C++11
-
<unordered_set>C++11
- <vector>
-
Input/Output:
Multi-threading:
-
<atomic>C++11
-
<condition_variable>C++11
-
<future>C++11
-
<mutex>C++11
-
<thread>C++11
-
Other:
- <algorithm>
- <bitset>
-
<chrono>C++11
-
<codecvt>C++11
- <complex>
- <exception>
- <functional>
-
<initializer_list>C++11
- <iterator>
- <limits>
- <locale>
- <memory>
- <new>
- <numeric>
-
<random>C++11
-
<ratio>C++11
-
<regex>C++11
- <stdexcept>
- <string>
-
<system_error>C++11
-
<tuple>C++11
-
<type_traits>C++11
-
<typeindex>C++11
- <typeinfo>
- <utility>
- <valarray>
<algorithm>
- adjacent_find
-
all_ofC++11
-
any_ofC++11
- binary_search
- copy
- copy_backward
-
copy_ifC++11
-
copy_nC++11
- count
- count_if
- equal
- equal_range
- fill
- fill_n
- find
- find_end
- find_first_of
- find_if
-
find_if_notC++11
- for_each
- generate
- generate_n
- includes
- inplace_merge
-
is_heapC++11
-
is_heap_untilC++11
-
is_partitionedC++11
-
is_permutationC++11
-
is_sortedC++11
-
is_sorted_untilC++11
- iter_swap
- lexicographical_compare
- lower_bound
- make_heap
- max
- max_element
- merge
- min
- min_element
-
minmaxC++11
-
minmax_elementC++11
- mismatch
-
moveC++11
-
move_backwardC++11
- next_permutation
-
none_ofC++11
- nth_element
- partial_sort
- partial_sort_copy
- partition
-
partition_copyC++11
-
partition_pointC++11
- pop_heap
- prev_permutation
- push_heap
- random_shuffle
- remove
- remove_copy
- remove_copy_if
- remove_if
- replace
- replace_copy
- replace_copy_if
- replace_if
- reverse
- reverse_copy
- rotate
- rotate_copy
- search
- search_n
- set_difference
- set_intersection
- set_symmetric_difference
- set_union
-
shuffleC++11
- sort
- sort_heap
- stable_partition
- stable_sort
- swap
- swap_ranges
- transform
- unique
- unique_copy
- upper_bound
- Reference
- <algorithm>
- upper_bound
function template
<algorithm>
std::upper_bound
default (1) | template <class ForwardIterator, class T> ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last, const T& val); |
---|---|
custom (2) | template <class ForwardIterator, class T, class Compare> ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last, const T& val, Compare comp); |
Return iterator to upper bound
[first,last)
which compares greater than val.The elements are compared using
operator<
for the first version, and comp for the second. The elements in the range shall already be sorted according to this same criterion (operator<
or comp), or at least partitioned with respect to val.The function optimizes the number of comparisons performed by comparing non-consecutive elements of the sorted range, which is specially efficient for random-access iterators.
Unlike lower_bound, the value pointed by the iterator returned by this function cannot be equivalent to val, only greater.
The behavior of this function template is equivalent to:
|
|
Parameters
- first, last
- Forward iterators to the initial and final positions of a sorted (or properly partitioned) sequence. The range used is
[first,last)
, which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. - val
- Value of the upper bound to search for in the range.
For (1), T shall be a type supporting being compared with elements of the range[first,last)
as the left-hand side operand ofoperator<
. - comp
- Binary function that accepts two arguments (the first is always val, and the second of the type pointed by ForwardIterator), and returns a value convertible to
bool
. The value returned indicates whether the first argument is considered to go before the second.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.
Return value
An iterator to the upper bound position for val in the range.If no element in the range compares greater than val, the function returns last.
Example
|
|
Output:
lower_bound at position 3 upper_bound at position 6 |
Complexity
On average, logarithmic in the distance between first and last: Performs approximatelylog2(N)+1
element comparisons (where N is this distance).On non-random-access iterators, the iterator advances produce themselves an additional linear complexity in N on average.
Data races
The objects in the range[first,last)
are accessed.Exceptions
Throws if either an element comparison or an operation on an iterator throws.Note that invalid arguments cause undefined behavior.
See also
- lower_bound
- Return iterator to lower bound (function template)
- equal_range
- Get subrange of equal elements (function template)
- binary_search
- Test if value exists in sorted sequence (function template)
- max_element
- Return largest element in range (function template)
Home page | Privacy policy
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us