Home C++ Algorithms Functions upper_bound() C++ std::upper_bound() Function Definition In C++, std::upper_bound() does a binary search in a sorted range and returns an iterator to the first element that is strictly greater than the value.
Together with lower_bound(), it lets you count how many times a value appears or find the end of a group of equal values.
Syntax Add #include <algorithm>, then call std::upper_bound(). Start with these two lines.
std::upper_bound(v.begin(), v.end(), 20);
std::upper_bound(v.begin(), v.end(), 70, std::greater<int>()); Call What it does std::upper_bound(v.begin(), v.end(), 20);First element greater than 20. std::upper_bound(v.begin(), v.end(), 70, std::greater<int>());Search a descending range.
Library declarations template<class ForwardIt, class T>
ForwardIt upper_bound(ForwardIt first, ForwardIt last, const T& value);
template<class ForwardIt, class T, class Compare>
ForwardIt upper_bound(ForwardIt first, ForwardIt last,
const T& value, Compare comp); Parameters Parameter Description first, last Required. A sorted range. value Required. The value to compare with. comp Optional. The same comparison that was used to sort the range.
Return Value Return Value Description Iterator Points at the first element greater than value. lastReturned when no element is greater.
C++ Examples 1. First greater value 2. Count duplicates 3. Descending order Example 1 — First greater value Find the first number above 20.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {10, 20, 20, 30};
auto it = std::upper_bound(v.begin(), v.end(), 20);
std::cout << "First value above 20 is " << *it << " at index " << (it - v.begin()) << '\n';
return 0;
} First value above 20 is 30 at index 3 How It Works Both 20s are skipped, because they are not greater than 20. 30 is the first greater value, at index 3. Check against v.end() first if there might be no greater value. Example 2 — Count duplicates Use both bounds to count equal values.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 2, 2, 3};
auto lo = std::lower_bound(v.begin(), v.end(), 2);
auto hi = std::upper_bound(v.begin(), v.end(), 2);
std::cout << "2 appears " << (hi - lo) << " times\n";
return 0;
} How It Works lower_bound() points at the first 2, at index 1.upper_bound() points just after the last 2, at index 4.The distance between them, 3, is the number of 2s. Example 3 — Descending order Use std::greater for data sorted from large to small.
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {90, 70, 70, 50};
auto it = std::upper_bound(v.begin(), v.end(), 70, std::greater<int>());
std::cout << "First value below 70 is " << *it << " at index " << (it - v.begin()) << '\n';
return 0;
} First value below 70 is 50 at index 3 How It Works The data is sorted from large to small, so we must pass the same std::greater<int>(). In that order, "greater" means "comes later", so the search skips both 70s. The first value after them is 50. Frequently Asked Questions What does std::upper_bound() do? In C++, std::upper_bound() finds the first element that is greater than a value in a sorted range. Include <algorithm> to use it.
Which header and C++ version do I need for std::upper_bound()? Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
What does std::upper_bound() return? It returns an iterator to the first element that is greater than the value, or the last iterator if there is none.
How do I count equal values in a sorted vector? Subtract lower_bound() from upper_bound(), or call std::equal_range() and subtract its two iterators.
Why must I pass std::greater for a descending range? The search must use the same order as the sort. Otherwise the binary search goes the wrong way.
Browse every algorithm Open the function index to pick your next C++ algorithm.
Function index → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
15 people found this page helpful
Helpful Share Copy link Suggestion