C++
'std::upper_bound' Is Not a Member of 'std' Fix
std::upper_bound lives in one header, <algorithm> - and on this library version almost no ordinary file rescues it by accident: in the sweep probed here only <functional> and <chrono> pull the name in on the side, and the textbook combination of <iostream> and <vector> does not. The error therefore surfaces in textbook files as well as lean ones, and in the sweep probed here it splits by compile result only two ways - the error (plain, or with a 'did you mean 'lower_bound'?' suggestion where a neighboring name is visible), or one of those two transitive rescues - with no link-failure shape behind any probed header. This guide pins down both outcomes, which probed headers cause each one, and the one include that fixes the error by specification.
Written December 1, 2026 · Tested with GCC 13.3.0 · 19 min read
- The error: the include is missing, in a lean file
- A neighboring header does not rescue it: only <utility>
- The textbook version fails too - with a suggestion the lean file does not get
- Neighboring headers: which ones fail, which ones rescue it
Written December 1, 2026
Tested with GCC 13.3.0
What this guide is
std::upper_bound is a function template defined in the header <algorithm>, since C++98. It takes a range [first, last), which needs only to be partitioned with respect to value and is usually sorted, and a value, and returns an iterator to the first element in the range that is ordered after value - the first element greater than value under the default ordering - or last if no such element exists. The default overload orders by operator< (by std::less{} since C++20); a second overload takes a comparator comp and orders by comp instead. Both overloads are constexpr since C++20, and each makes at most log2(N)+O(1) comparisons for a range of N elements. Where std::lower_bound finds the first element not less than the value, std::upper_bound finds the first element greater than it, so the two together bracket every element equal to the value.
Because the template lives in one specific header, 'upper_bound' is not a member of 'std' has one common cause:
- The
#include <algorithm>line is missing, and no other header in the file pulls the name in transitively. For this error, in the lean contexts probed here, GCC prints no note naming<algorithm>and no suggestion, so the fix has to come from knowing the name's home, not from reading a note. The neighboring names probed here -std::lower_bound,std::equal_range, andstd::binary_search, all in the same header - are silent the same way in the same lean context, as the neighboring-names section shows. One context probed here is different: the textbook file below gets adid you meansuggestion, and that section explains why.
What makes this error worth separating from its relatives is the shape of the sweep behind it. In the sweep probed here the outcome splits by compile result only two ways - the compile error (plain in most failing contexts, with a 'did you mean' suggestion in the contexts where a neighboring name is visible), or a transitive rescue - and the rescue side is unusually small: only two probed headers besides <algorithm> itself rescue this name, the same two that rescued std::clamp in its sweep. The older siblings in this family - std::lower_bound, std::sort, std::find, and std::min - have their own guides and are linked at the end, not re-taught here.
The error: the include is missing, in a lean file
This program searches a sorted array for the first element greater than 5 with std::upper_bound and checks that it sits at index 5, but includes only <limits>:
#include <limits>
int main() {
int a[] = {1, 2, 4, 5, 5, 6};
return std::upper_bound(a, a + 6, 5) - a == 5 ? 0 : 1;
}
$ g++ -std=c++20 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:4:17: error: 'upper_bound' is not a member of 'std'
4 | return std::upper_bound(a, a + 6, 5) - a == 5 ? 0 : 1;
| ^~~~~~~~~~~
That is the whole output: one error, no note, no suggestion. The same program with no include at all fails the same way:
int main() {
int a[] = {1, 2, 4, 5, 5, 6};
return std::upper_bound(a, a + 6, 5) - a == 5 ? 0 : 1;
}
$ g++ -std=c++20 n_none.cpp -o n_none_prog
n_none.cpp: In function 'int main()':
n_none.cpp:3:17: error: 'upper_bound' is not a member of 'std'
3 | return std::upper_bound(a, a + 6, 5) - a == 5 ? 0 : 1;
| ^~~~~~~~~~~
A neighboring header does not rescue it: only <utility>
A reader coming from the std::swap error might reach for <utility> first. In the program probed here it changes nothing - the same plain error, at the same position:
#include <utility>
int main() {
int a[] = {1, 2, 4, 5, 5, 6};
return std::upper_bound(a, a + 6, 5) - a == 5 ? 0 : 1;
}
$ g++ -std=c++20 u_utility.cpp -o u_utility_prog
u_utility.cpp: In function 'int main()':
u_utility.cpp:4:17: error: 'upper_bound' is not a member of 'std'
4 | return std::upper_bound(a, a + 6, 5) - a == 5 ? 0 : 1;
| ^~~~~~~~~~~
That is expected once the homes are separated: <utility> declares std::swap, and std::upper_bound is not one of its names. The two errors share a shape and a sentence pattern, but each name takes its own header, and the fix below is <algorithm>, not the swap fix.
The textbook version fails too - with a suggestion the lean file does not get
The version most often written first - search a vector and print where the first element greater than 5 sits, with std::cout - gets the same error, without <algorithm>:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 4, 5, 5, 6};
std::cout << std::upper_bound(v.begin(), v.end(), 5) - v.begin() << "\n";
return 0;
}
$ g++ -std=c++20 t_textbook.cpp -o t_textbook_prog
t_textbook.cpp: In function 'int main()':
t_textbook.cpp:5:23: error: 'upper_bound' is not a member of 'std'; did you mean 'lower_bound'?
5 | std::cout << std::upper_bound(v.begin(), v.end(), 5) - v.begin() << "\n";
| ^~~~~~~~~~~
| lower_bound
What happened: on this library version, neither <vector> nor <iostream> pulls this name in transitively, so the call fails in a textbook file just as it does in a lean one. The one difference from the lean file is the tail of the error line: here GCC adds did you mean 'lower_bound'? and underlines that neighboring name, because in this context std::lower_bound is visible - the same tail appears with any sole header that makes std::lower_bound visible without declaring std::upper_bound, among them <vector>, <string>, <iostream>, <memory>, <array>, <map>, <set>, <thread>, <stdexcept>, <iterator>, and <system_error> - and the suggestion names a different function that would answer a different question (the first element not less than 5, at index 3, not the first element greater than 5, at index 5). Do not take the suggestion as the fix: the fix for the name the program means is still the include below. In the sweep probed here, the only headers that rescue std::upper_bound are <functional> and <chrono>, besides <algorithm> itself, and that rescue is a property of this library version, not a rule of the language. A program that uses std::upper_bound includes <algorithm>, whether or not it happens to compile without it.
Neighboring headers: which ones fail, which ones rescue it
In a sweep where each header below was compiled as the only header in a program that searches a sorted array for the first element greater than 5 and returns whether it sits at index 5, the outcome splits by compile result two ways, and the failing side comes in two forms. These headers fail with the plain is not a member of 'std' error, with no note and no suggestion:
<limits><cstring><cstdint><cstddef><initializer_list><version><climits><cctype><new><typeinfo><type_traits><compare><concepts><ratio><bit><optional><numeric><tuple><mutex><exception><utility>
And these headers fail with the same error and no note, but with a did you mean 'lower_bound'? suggestion on the error line - because each of them makes std::lower_bound visible without declaring std::upper_bound:
<vector><string><iostream><memory><array><map><set><thread><stdexcept><iterator><system_error>
A file with no include at all fails the same way. And these headers rescue the program - it compiles and runs with no <algorithm> anywhere in the file:
<algorithm><functional><chrono>
Two contrasts with the neighboring sweeps are worth holding on to. First, the split here is two-way: in the sweep probed here, every failing sole header fails at compile time - plainly, or with the lower_bound suggestion where that name is visible - each one was also compiled to an object file on its own, and that step failed the same way - so the declared-but-not-defined link failure that std::swap produces behind five headers has no counterpart for this name in the probed set. Second, the rescue list matches std::clamp's short list, not std::lower_bound's longer one: <vector>, <string>, <iostream>, <memory>, <array>, <map>, <set>, <thread>, <stdexcept>, <iterator>, and <system_error> rescued std::lower_bound in its sweep and fail here, while <algorithm> - the home header - is on this list by definition, not by transitivity. Every other rescue on that list is this library version's behavior, not a guarantee.
The fix
Add the header:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 4, 5, 5, 6};
std::cout << std::upper_bound(v.begin(), v.end(), 5) - v.begin() << "\n";
return 0;
}
$ g++ -std=c++20 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
5
One include, <algorithm>, declares the template. In the sorted vector 1, 2, 4, 5, 5, 6 the first element greater than 5 is 6, at index 5, so the program prints 5.
The fixed programs: positions across the range
With <algorithm> added, std::upper_bound answers for values below, inside, and above the range. In the same sorted vector, the first element greater than 0 sits at index 0, the first greater than 2 at index 2, the first greater than 5 at index 5 - past both copies of 5 - and the first greater than 6 at index 6, one past the end:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 4, 5, 5, 6};
for (int x : {0, 2, 5, 6}) std::cout << std::upper_bound(v.begin(), v.end(), x) - v.begin() << " ";
std::cout << "\n";
return 0;
}
$ g++ -std=c++20 i_cases.cpp -o i_cases_prog
$ ./i_cases_prog
0 2 5 6
Dereferencing the returned iterator gives the element itself, and a search for a value above every element returns the end iterator. The first element greater than 5 is 6, and the search for 6 reports that it reached the end - printed as 1, for true:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 4, 5, 5, 6};
auto it = std::upper_bound(v.begin(), v.end(), 5);
std::cout << *it << " " << (std::upper_bound(v.begin(), v.end(), 6) == v.end()) << "\n";
return 0;
}
$ g++ -std=c++20 k_values.cpp -o k_values_prog
$ ./k_values_prog
6 1
The comparator overload orders by the comparator instead of operator<. With std::greater<int> the range is written in descending order - 6, 5, 5, 4, 2, 1 - and the first element ordered after 5 under that reversed ordering sits at index 3, past both copies of 5, while the first element ordered after 3 sits at index 4:
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {6, 5, 5, 4, 2, 1};
std::cout << std::upper_bound(v.begin(), v.end(), 5, std::greater<int>()) - v.begin() << " " << std::upper_bound(v.begin(), v.end(), 3, std::greater<int>()) - v.begin() << "\n";
return 0;
}
$ g++ -std=c++20 j_comp.cpp -o j_comp_prog
$ ./j_comp_prog
3 4
The neighboring names: same header, same silence
The binary-search names that std::upper_bound is usually paired with share its home header and its silence. std::lower_bound - the function template defined in <algorithm> that returns an iterator to the first element not less than the value - gets its own error, again with no header note:
#include <limits>
int main() {
int a[] = {1, 2, 4, 5, 5, 6};
return std::lower_bound(a, a + 6, 5) - a == 3 ? 0 : 1;
}
$ g++ -std=c++20 b_lower.cpp -o b_lower_prog
b_lower.cpp: In function 'int main()':
b_lower.cpp:4:17: error: 'lower_bound' is not a member of 'std'
4 | return std::lower_bound(a, a + 6, 5) - a == 3 ? 0 : 1;
| ^~~~~~~~~~~
std::equal_range - the function template defined in <algorithm> that returns the pair of bounds bracketing the elements equal to the value - fails the same way:
#include <limits>
int main() {
int a[] = {1, 2, 4, 5, 5, 6};
auto r = std::equal_range(a, a + 6, 5);
return r.second - r.first == 2 ? 0 : 1;
}
$ g++ -std=c++20 c_equal.cpp -o c_equal_prog
c_equal.cpp: In function 'int main()':
c_equal.cpp:4:19: error: 'equal_range' is not a member of 'std'
4 | auto r = std::equal_range(a, a + 6, 5);
| ^~~~~~~~~~~
std::binary_search - the function template defined in <algorithm> that reports whether an element equal to the value is present - fails the same way:
#include <limits>
int main() {
int a[] = {1, 2, 4, 5, 5, 6};
return std::binary_search(a, a + 6, 5) ? 0 : 1;
}
$ g++ -std=c++20 d_binary.cpp -o d_binary_prog
d_binary.cpp: In function 'int main()':
d_binary.cpp:4:17: error: 'binary_search' is not a member of 'std'
4 | return std::binary_search(a, a + 6, 5) ? 0 : 1;
| ^~~~~~~~~~~~~
One contrast is worth holding on to: in the lean programs here, upper_bound, lower_bound, equal_range, and binary_search all get no note. Do not read the absence of a note as a puzzle to solve - in this probed set the note is simply absent for this header's names in this context, and the fix comes from the names' home, <algorithm>. The textbook file above is the exception that proves the shape: a suggestion appears there only because a neighboring name happens to be visible in that context, and it names the wrong function.
The neighboring names, fixed
And the neighboring names work under the same one added header: std::lower_bound for 5 sits at index 3, the std::equal_range for 5 spans 2 elements, std::binary_search reports 5 present - printed as 1 - and 3 absent - printed as 0:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 4, 5, 5, 6};
auto r = std::equal_range(v.begin(), v.end(), 5);
std::cout << std::lower_bound(v.begin(), v.end(), 5) - v.begin() << " " << (r.second - r.first) << " " << std::binary_search(v.begin(), v.end(), 5) << " " << std::binary_search(v.begin(), v.end(), 3) << "\n";
return 0;
}
$ g++ -std=c++20 k_neighbors_fixed.cpp -o k_neighbors_fixed_prog
$ ./k_neighbors_fixed_prog
3 2 1 0
Since C++20 the same <algorithm> header also declares std::ranges::upper_bound, the ranges form of the same search. With <algorithm> included it takes the range itself instead of an iterator pair and finds the first element greater than 5 at index 5; in the lean context - only <limits> included - it fails with its own wording, 'std::ranges' has not been declared, because the namespace itself is not declared there:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 4, 5, 5, 6};
std::cout << std::ranges::upper_bound(v, 5) - v.begin() << "\n";
return 0;
}
$ g++ -std=c++20 r_ranges.cpp -o r_ranges_prog
$ ./r_ranges_prog
5
#include <limits>
int main() {
int a[] = {1, 2, 4, 5, 5, 6};
return std::ranges::upper_bound(a, 5) - a == 5 ? 0 : 1;
}
$ g++ -std=c++20 r2_ranges_lean.cpp -o r2_ranges_lean_prog
r2_ranges_lean.cpp: In function 'int main()':
r2_ranges_lean.cpp:4:17: error: 'std::ranges' has not been declared
4 | return std::ranges::upper_bound(a, 5) - a == 5 ? 0 : 1;
| ^~~~~~
A different wording - and a case where the qualifier is not needed
Including <algorithm> is not the only half of the name - but for this function the unqualified form behaves two ways, and which one a program gets depends on its arguments. This program includes the header, drops the std:: qualifier, and passes raw pointers into a C array:
#include <algorithm>
int main() {
int a[] = {1, 2, 4, 5, 5, 6};
return upper_bound(a, a + 6, 5) - a == 5 ? 0 : 1;
}
$ g++ -std=c++20 g2_unqualified_ptr.cpp -o g2_unqualified_ptr_prog
g2_unqualified_ptr.cpp: In function 'int main()':
g2_unqualified_ptr.cpp:4:12: error: 'upper_bound' was not declared in this scope
4 | return upper_bound(a, a + 6, 5) - a == 5 ? 0 : 1;
| ^~~~~~~~~~~
g2_unqualified_ptr.cpp:4:12: note: suggested alternatives:
In file included from /usr/include/c++/13/algorithm:61,
from g2_unqualified_ptr.cpp:1:
/usr/include/c++/13/bits/stl_algo.h:2092:5: note: 'std::upper_bound'
2092 | upper_bound(_ForwardIterator __first, _ForwardIterator __last,
| ^~~~~~~~~~~
In file included from /usr/include/c++/13/algorithm:63:
/usr/include/c++/13/bits/ranges_algo.h:2135:37: note: 'std::ranges::upper_bound'
2135 | inline constexpr __upper_bound_fn upper_bound{};
| ^~~~~~~~~~~
That wording - was not declared in this scope - is a different defect with a different fix: the header is present, the qualifier is not, and raw pointers belong to no namespace, so no lookup finds the name. Here the suggestions do name the right names - the first points at std::upper_bound itself, in the internal header the transcript names, and the second at std::ranges::upper_bound. Qualify the name as std::upper_bound; adding includes does not bring the name out of namespace std.
The same unqualified call with std::vector iterators as its arguments behaves differently - it compiles and runs, printing 5:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 4, 5, 5, 6};
std::cout << upper_bound(v.begin(), v.end(), 5) - v.begin() << "\n";
return 0;
}
$ g++ -std=c++20 g_unqualified.cpp -o g_unqualified_prog
$ ./g_unqualified_prog
5
What happened: the iterators belong to namespace std, so argument-dependent lookup finds std::upper_bound from the arguments themselves, even without the qualifier. That is a property of the argument types, not a style to rely on: the same call over raw pointers fails, as the transcript above shows. Write the qualifier either way.
The exact rule
std::upper_boundis declared by#include <algorithm>, and by no other header by rule. In the sweep probed here,<limits>,<cstring>,<cstdint>,<cstddef>,<initializer_list>,<version>,<climits>,<cctype>,<new>,<typeinfo>,<type_traits>,<compare>,<concepts>,<ratio>,<bit>,<optional>,<numeric>,<tuple>,<mutex>,<exception>,<utility>,<vector>,<string>,<iostream>,<memory>,<array>,<map>,<set>,<thread>,<stdexcept>,<iterator>, and<system_error>do not declare it, and neither does a file with no include at all.'upper_bound' is not a member of 'std'means the<algorithm>include is missing and nothing else in the file pulled the name in - and, in the lean contexts probed here, GCC prints no note naming<algorithm>and no suggestion under it. In the textbook context probed here the error line adds adid you mean 'lower_bound'?suggestion, which names a different function, not the missing header.- In the sweep probed here, the split by compile result is two-way: a sole header either fails with that compile error - plainly, or with a
did you mean 'lower_bound'?suggestion on the error line where the header makesstd::lower_boundvisible - or rescues the program. Each failing sole header also failed when compiled to an object file on its own, so the probed set contains no declared-but-not-defined link-failure shape for this name. - In the sweep probed here, only two headers besides
<algorithm>rescuestd::upper_boundtransitively -<functional>and<chrono>- so a file whose only include is one of those compiles and runs the call without<algorithm>. That is this library version's behavior, not a guarantee - in the probed set, those headers are not required to pullstd::upper_boundin. std::lower_bound,std::equal_range, andstd::binary_searchare declared by the same#include <algorithm>: in the programs shown here, which include only<limits>beside the use, each gets its ownis not a member of 'std'error when that include is missing, with no header note.std::ranges::upper_boundis declared by the same#include <algorithm>since C++20: with the include it searches the range passed to it, and in the lean context probed here it gets the error'std::ranges' has not been declaredwhen that include is missing.'upper_bound' was not declared in this scopefor an unqualifiedupper_bounduse over raw pointers is a different defect: qualify the name asstd::upper_bound- the include alone does not bring the name out of namespacestd. The same unqualified use overstd::vectoriterators compiles in the program probed here through argument-dependent lookup; qualify the name there too, so the call does not depend on its argument types.
The fixes, ranked
- First, check the include list of the file that names
std::upper_bound. If<algorithm>is not there, add it - that is the fix in the common case, and it holds whether the file failed at compile time or happened to compile before through a transitive rescue. - Second, do not follow a
did you meansuggestion blindly. In the textbook program probed here the suggestion namesstd::lower_bound, a different function answering a different question - take the fix from the name the program means and its home header,<algorithm>. - Third, do not reach for
<utility>because the error sentence looks like thestd::swaperror. In the program probed here,<utility>as the only include fails with the same plain error - the two names live in different headers, and each takes its own. - Fourth, do not be reassured by
<iostream>,<vector>, or<string>being present - in the sweep here none of them rescues this name at all - and do not remove<algorithm>because a file compiles without it: in the probed set only<functional>and<chrono>rescue it, and that rescue is not guaranteed. - Fifth, if the error says
was not declared in this scopeinstead, fix the name's qualifier tostd::upper_boundas well - and do not rely on an unqualified call that happens to compile overstditerators, because the same call over raw pointers fails. - Sixth, if the failing name is a neighboring one, check its header before fixing:
lower_bound,equal_range, andbinary_searchtake the same added<algorithm>, whileswaptakes<utility>.
Which fix for which cause
#include <algorithm>missing,std::upper_boundused in a file whose only include is<limits>,<cstring>,<cstdint>,<cstddef>,<initializer_list>,<version>,<climits>,<cctype>,<new>,<typeinfo>,<type_traits>,<compare>,<concepts>,<ratio>,<bit>,<optional>,<numeric>,<tuple>,<mutex>,<exception>, or<utility>, or that includes nothing: the message is'upper_bound' is not a member of 'std', with no header note and no suggestion - add#include <algorithm>.#include <algorithm>missing,std::upper_boundused in a file whose only include is<vector>,<string>,<iostream>,<memory>,<array>,<map>,<set>,<thread>,<stdexcept>,<iterator>, or<system_error>: the message is the same error with adid you mean 'lower_bound'?suggestion on the error line and no header note - add#include <algorithm>; do not switch the call to the suggested function.std::upper_boundused in a textbook file including<iostream>and<vector>: the message is the same error with adid you mean 'lower_bound'?suggestion on the error line - add#include <algorithm>; do not switch the call to the suggested function.std::upper_boundused with only<algorithm>,<functional>, or<chrono>included: no error on this library version - add#include <algorithm>anyway when it is not the include already present, because the rescue is not guaranteed.std::lower_bound,std::equal_range, orstd::binary_searchused without<algorithm>in the lean context: the message names that name, with no header note - add#include <algorithm>.std::ranges::upper_boundused without<algorithm>in the lean context: the message is'std::ranges' has not been declared- add#include <algorithm>.upper_boundused without thestd::qualifier,<algorithm>included, over raw pointers: the message is'upper_bound' was not declared in this scope- qualify the name asstd::upper_bound. Overstd::vectoriterators the same unqualified call compiles through argument-dependent lookup in the program probed here - qualify it anyway.
Checklist
- Check the include list of the file that names the search, not just the project. The include must be in that file, or in a header that file includes directly by design.
- Do not wait for a note naming the header. For this error, in the lean contexts probed here, GCC prints none for
upper_bound- take the fix from the name's home header,<algorithm>, instead. Adid you meansuggestion, where one appears, names a neighboring function, not the header. - Do not import the
std::swapfix into this error: in the probed set,<utility>fails here the same plain way as<limits>does. Split neighboring names by home header first. - Do not assume the textbook includes rescue this name, and do not delete a working
<algorithm>line because a file compiles without it. In the sweep here<iostream>and<vector>do not rescuestd::upper_boundat all, and the two headers that do rescue it do so only transitively. - One header per name the file uses:
std::vectorcomes from<vector>andstd::stringfrom<string>, even in a program whose subject is the search - andstd::upper_boundcomes from<algorithm>in return. - After the fix, confirm the program runs, not just that it compiles: the fixed programs here print
5for the first element greater than5in the sorted vector, and6 1for the dereferenced result and the past-the-end check.
Related guides
- 'std::lower_bound' Is Not a Member of 'std' Fix - the same missing-include shape for the other binary-search bound in
<algorithm>: its sweep splits two ways like this guide's, but its rescue list is far longer, and its textbook file is rescued where this guide's is not. - 'std::sort' Is Not a Member of 'std' Fix - the same missing-include shape for the sorting function template in
<algorithm>, an older sibling in this family whose own guide covers its error and its fix. - 'std::find' Is Not a Member of 'std' Fix - the same missing-include shape for the linear-search function template in
<algorithm>, an older sibling in this family. - 'std::min' Is Not a Member of 'std' Fix - the same missing-include shape for the smaller-of-two function template in
<algorithm>, an older sibling in this family.
Sources
- cppreference,
std::upper_bound— forstd::upper_boundbeing a function template defined in<algorithm>since C++98 that returns an iterator to the first element in[first, last)ordered aftervalue, orlastif no such element exists, with one overload ordering byoperator<(bystd::less{}since C++20) and a second overload ordering by a comparatorcomp, both overloadsconstexprsince C++20, at most log2(N)+O(1) comparisons, and the range needing only to be partitioned with respect tovalue, though it is usually sorted. - cppreference, Standard library header
<algorithm>— for<algorithm>being the header that declaresstd::upper_boundand the neighboring algorithm names, including thestd::ranges::upper_boundalgorithm function object since C++20. - cppreference,
std::lower_bound— forstd::lower_boundbeing a function template defined in<algorithm>that returns an iterator to the first element not less than the value. - cppreference,
std::equal_range— forstd::equal_rangebeing a function template defined in<algorithm>that returns the pair of bounds bracketing the elements equal to the value. - cppreference,
std::binary_search— forstd::binary_searchbeing a function template defined in<algorithm>that reports whether an element equal to the value is present in a sorted range.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.