C++
'std::lower_bound' Is Not a Member of 'std' Fix
std::lower_bound lives in one header, <algorithm> - but on this library version many ordinary files never see this error, because a set of common headers pulls the name in on the side. It surfaces in lean files, and in the sweep probed here it splits only two ways - the plain error, or a transitive rescue - 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 · 15 min read
- The error: the include is missing, in a lean file
- A neighboring header does not rescue it: only <utility>
- The textbook version does not fail - some common headers rescue it
- Neighboring headers: which ones fail, which ones rescue it
Written December 1, 2026
Tested with GCC 13.3.0
What this guide is
std::lower_bound is a function template defined in the header <algorithm>. It searches the range [first, last) and returns an iterator to the first element that is not ordered before the given value - the first element not less than the value, for the overload that orders by operator< - or last when every element is ordered before the value. A second overload takes a comparator comp and orders by comp instead. Both overloads are constexpr since C++20. The range only has to be partitioned with respect to the value, and in practice the call is used on a sorted range, where the returned iterator is the position at which the value could be inserted without breaking the order.
Because the template lives in one specific header, 'lower_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 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 binary-search names probed here -std::upper_bound,std::binary_search, andstd::equal_range, all in the same header - are silent the same way, as the neighboring-names section shows.
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 only two ways - the plain compile error, or a transitive rescue - and one of the headers that fails is <utility>, the home of std::swap: the include that fixes the neighboring std::swap error does nothing for this name. Each case is separated below. The older siblings in this family - 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 not less than 3 with std::lower_bound, but includes only <limits>:
#include <limits>
int main() {
int a[] = {1, 2, 4, 5};
auto it = std::lower_bound(a, a + 4, 3);
return it != a + 4 && *it == 4 && it - a == 2 ? 0 : 1;
}
$ g++ -std=c++20 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:4:20: error: 'lower_bound' is not a member of 'std'
4 | auto it = std::lower_bound(a, a + 4, 3);
| ^~~~~~~~~~~
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};
auto it = std::lower_bound(a, a + 4, 3);
return it != a + 4 && *it == 4 && it - a == 2 ? 0 : 1;
}
$ g++ -std=c++20 n_none.cpp -o n_none_prog
n_none.cpp: In function 'int main()':
n_none.cpp:3:20: error: 'lower_bound' is not a member of 'std'
3 | auto it = std::lower_bound(a, a + 4, 3);
| ^~~~~~~~~~~
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};
auto it = std::lower_bound(a, a + 4, 3);
return it != a + 4 && *it == 4 && it - a == 2 ? 0 : 1;
}
$ g++ -std=c++20 u_utility.cpp -o u_utility_prog
u_utility.cpp: In function 'int main()':
u_utility.cpp:4:20: error: 'lower_bound' is not a member of 'std'
4 | auto it = std::lower_bound(a, a + 4, 3);
| ^~~~~~~~~~~
That is expected once the homes are separated: <utility> declares std::swap, and std::lower_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 does not fail - some common headers rescue it
The version most often written first - search a vector and print the found value and its index with std::cout - compiles and runs without <algorithm>:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 4, 5, 5, 6};
auto it = std::lower_bound(v.begin(), v.end(), 3);
std::cout << *it << " " << (it - v.begin()) << "\n";
return 0;
}
$ g++ -std=c++20 t_textbook.cpp -o t_textbook_prog
$ ./t_textbook_prog
4 2
What happened: on this library version, <vector> and <iostream> each pull the name in transitively, so the call succeeds in a textbook file even though neither header is required to declare std::lower_bound. That rescue is a property of this library version, not a rule of the language - in the sweep probed here, the only header whose declaration of std::lower_bound is the defined one is <algorithm>, and another library could leave the name out of the rescuing headers. A program that uses std::lower_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 not less than 3 and returns whether it found 4 at index 2, the outcome splits two ways. 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>
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><vector><string><iostream><memory><array><functional><map><set><thread><chrono><stdexcept><iterator><system_error>
Two contrasts with the neighboring std::swap sweep 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 with the plain error - 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 is shorter and different: <optional>, <numeric>, <tuple>, and <utility> rescued std::swap 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>
int main() {
int a[] = {1, 2, 4, 5};
auto it = std::lower_bound(a, a + 4, 3);
std::cout << *it << " " << (it - a) << "\n";
return 0;
}
$ g++ -std=c++20 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
4 2
One include, <algorithm>, declares the template. std::lower_bound(a, a + 4, 3) returns an iterator to the first element not less than 3, which is 4 at index 2, so the program prints 4 2.
The neighboring names: same header, same silence
The binary-search names that sit next to std::lower_bound share its home header and its silence. std::upper_bound - the function template defined in <algorithm> that finds the first element greater than the given value - gets its own error, again with no header note:
#include <limits>
int main() {
int a[] = {1, 2, 4, 5};
auto it = std::upper_bound(a, a + 4, 4);
return it != a + 4 && *it == 5 ? 0 : 1;
}
$ g++ -std=c++20 b_upper.cpp -o b_upper_prog
b_upper.cpp: In function 'int main()':
b_upper.cpp:4:20: error: 'upper_bound' is not a member of 'std'
4 | auto it = std::upper_bound(a, a + 4, 4);
| ^~~~~~~~~~~
std::binary_search - the function template defined in <algorithm> that reports whether an element equivalent to the given value exists in the range - fails the same way:
#include <limits>
int main() {
int a[] = {1, 2, 4, 5};
return std::binary_search(a, a + 4, 4) ? 0 : 1;
}
$ g++ -std=c++20 c_binary.cpp -o c_binary_prog
c_binary.cpp: In function 'int main()':
c_binary.cpp:4:17: error: 'binary_search' is not a member of 'std'
4 | return std::binary_search(a, a + 4, 4) ? 0 : 1;
| ^~~~~~~~~~~~~
And std::equal_range - the function template defined in <algorithm> that finds the range of elements matching the given value - fails the same way:
#include <limits>
int main() {
int a[] = {1, 2, 4, 5, 5};
auto r = std::equal_range(a, a + 5, 5);
return r.second - r.first == 2 ? 0 : 1;
}
$ g++ -std=c++20 d_equal_range.cpp -o d_equal_range_prog
d_equal_range.cpp: In function 'int main()':
d_equal_range.cpp:4:19: error: 'equal_range' is not a member of 'std'
4 | auto r = std::equal_range(a, a + 5, 5);
| ^~~~~~~~~~~
One contrast is worth holding on to: in the programs here, lower_bound, upper_bound, binary_search, and equal_range 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 binary-search names, and the fix comes from the names' home, <algorithm>.
The neighboring names, fixed
With <algorithm> added, std::lower_bound handles the cases a sorted search is actually for. On the vector 1 2 4 5 5 6, searching for 5 returns the first of the two equal elements, at index 3, and searching for the absent value 3 returns 4 at index 2 - the position where 3 would be inserted:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 4, 5, 5, 6};
auto it1 = std::lower_bound(v.begin(), v.end(), 5);
std::cout << *it1 << " " << (it1 - v.begin()) << "\n";
auto it2 = std::lower_bound(v.begin(), v.end(), 3);
std::cout << *it2 << " " << (it2 - v.begin()) << "\n";
return 0;
}
$ g++ -std=c++20 i_vector_dupes.cpp -o i_vector_dupes_prog
$ ./i_vector_dupes_prog
5 3
4 2
The comparator overload orders by the comparator instead of operator<. On the descending array 9 7 5 3, with a comparator that orders larger values first, searching for 6 returns 5 at index 2 - the first element not ordered before 6 under that ordering:
#include <algorithm>
#include <iostream>
int main() {
int a[] = {9, 7, 5, 3};
auto it = std::lower_bound(a, a + 4, 6, [](int x, int y) { return x > y; });
std::cout << *it << " " << (it - a) << "\n";
return 0;
}
$ g++ -std=c++20 j_comp.cpp -o j_comp_prog
$ ./j_comp_prog
5 2
And the neighboring names work under the same one added header: std::upper_bound for 4 returns 5 at index 3; std::binary_search reports 1 for the present value 4 and 0 for the absent value 3; and std::equal_range for 5 spans the two equal elements, a count of 2:
#include <algorithm>
#include <iostream>
int main() {
int a[] = {1, 2, 4, 5, 5};
auto up = std::upper_bound(a, a + 5, 4);
std::cout << *up << " " << (up - a) << "\n";
std::cout << std::binary_search(a, a + 5, 4) << " " << std::binary_search(a, a + 5, 3) << "\n";
auto r = std::equal_range(a, a + 5, 5);
std::cout << (r.second - r.first) << "\n";
return 0;
}
$ g++ -std=c++20 k_neighbors_fixed.cpp -o k_neighbors_fixed_prog
$ ./k_neighbors_fixed_prog
5 3
1 0
2
Since C++20 the same <algorithm> header also declares std::ranges::lower_bound, the ranges form of the same search. With <algorithm> included it finds 4 at index 2; 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>
int main() {
int a[] = {1, 2, 4, 5};
auto it = std::ranges::lower_bound(a, a + 4, 3);
std::cout << *it << " " << (it - a) << "\n";
return 0;
}
$ g++ -std=c++20 r_ranges.cpp -o r_ranges_prog
$ ./r_ranges_prog
4 2
#include <limits>
int main() {
int a[] = {1, 2, 4, 5};
auto it = std::ranges::lower_bound(a, a + 4, 3);
return it != a + 4 && *it == 4 ? 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:20: error: 'std::ranges' has not been declared
4 | auto it = std::ranges::lower_bound(a, a + 4, 3);
| ^~~~~~
A different wording: the name without its qualifier
Including <algorithm> is not the only half of the name. This program includes the header but drops the std:: qualifier:
#include <algorithm>
#include <iostream>
int main() {
int a[] = {1, 2, 4, 5};
auto it = lower_bound(a, a + 4, 3);
std::cout << *it << "\n";
return 0;
}
$ g++ -std=c++20 g_unqualified.cpp -o g_unqualified_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:5:15: error: 'lower_bound' was not declared in this scope
5 | auto it = lower_bound(a, a + 4, 3);
| ^~~~~~~~~~~
g_unqualified.cpp:5:15: note: suggested alternatives:
In file included from /usr/include/c++/13/algorithm:61,
from g_unqualified.cpp:1:
/usr/include/c++/13/bits/stl_algo.h:2005:5: note: 'std::lower_bound'
2005 | lower_bound(_ForwardIterator __first, _ForwardIterator __last,
| ^~~~~~~~~~~
In file included from /usr/include/c++/13/algorithm:63:
/usr/include/c++/13/bits/ranges_algo.h:2091:37: note: 'std::ranges::lower_bound'
2091 | inline constexpr __lower_bound_fn lower_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. Qualify the name as std::lower_bound; adding includes does not bring the name out of namespace std. Here the suggestions do name the right names - the first points at std::lower_bound itself, in the internal header the transcript names, and the second at std::ranges::lower_bound - and the qualifier is still the fix either way.
The exact rule
std::lower_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>, and<utility>do not declare it, and neither does a file with no include at all.'lower_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 contexts probed here, GCC prints no note naming<algorithm>and no suggestion under it.- In the sweep probed here, the split is two-way: a sole header either fails with that plain compile error 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, thirteen headers besides
<algorithm>rescuestd::lower_boundtransitively - among them<vector>,<string>, and<iostream>- 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::lower_boundin. std::upper_bound,std::binary_search, andstd::equal_rangeare 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::lower_boundis declared by the same#include <algorithm>since C++20: with the include it searches the range passed as arguments, and in the lean context probed here it gets the error'std::ranges' has not been declaredwhen that include is missing.'lower_bound' was not declared in this scopefor an unqualifiedlower_bounduse is a different defect: qualify the name asstd::lower_bound- the include alone does not bring the name out of namespacestd.
The fixes, ranked
- First, check the include list of the file that names
std::lower_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 wait for a note naming the header. For this error, in the contexts probed here, GCC prints none for
lower_bounditself - take the fix from the name's home header,<algorithm>. The neighboring binary-search names are silent the same way when they are the ones failing. - 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, and do not remove<algorithm>because the file compiles without it. In the sweep here those headers rescue the name only transitively, and the rescue is not guaranteed. - Fifth, if the error says
was not declared in this scopeinstead, fix the name's qualifier tostd::lower_boundas well. - Sixth, if the failing name is a neighboring one, check its header before fixing:
upper_bound,binary_search, andequal_rangetake the same added<algorithm>, whileswaptakes<utility>.
Which fix for which cause
#include <algorithm>missing,std::lower_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'lower_bound' is not a member of 'std', with no header note and no suggestion - add#include <algorithm>.std::lower_boundused with only<algorithm>,<vector>,<string>,<iostream>,<memory>,<array>,<functional>,<map>,<set>,<thread>,<chrono>,<stdexcept>,<iterator>, or<system_error>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::upper_bound,std::binary_search, orstd::equal_rangeused without<algorithm>in the lean context: the message names that name, with no header note - add#include <algorithm>.std::ranges::lower_boundused without<algorithm>in the lean context: the message is'std::ranges' has not been declared- add#include <algorithm>.lower_boundused without thestd::qualifier,<algorithm>included: the message is'lower_bound' was not declared in this scope- qualify the name asstd::lower_bound.
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 contexts probed here, GCC prints none for
lower_bound- take the fix from the name's home header,<algorithm>, instead. - 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 be reassured by the textbook includes being present, and do not delete a working
<algorithm>line because a file compiles without it. In the sweep here the common headers rescuestd::lower_boundonly 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::lower_boundcomes from<algorithm>in return. - After the fix, confirm the program runs, not just that it compiles: the fixed programs here print
4 2for the array search,5 3and4 2for the vector searches, and5 2for the descending comparator search.
Related guides
- '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. - 'std::swap' Is Not a Member of 'std' Fix - the neighboring missing-include shape in the other header,
<utility>: its sweep splits three ways where this guide's splits two, and its fix does not fix this guide's name.
Sources
- cppreference,
std::lower_bound— forstd::lower_boundbeing a function template defined in<algorithm>that searches the range[first, last)for the first element not ordered before the given value, returning that iterator orlastwhen no such element is found, 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 note that the call is usually used on a sorted range even though the range only has to be partitioned with respect to the value. - cppreference, Standard library header
<algorithm>— for<algorithm>being the header that declaresstd::lower_boundand the neighboring algorithm names, including thestd::ranges::lower_boundalgorithm function object since C++20. - cppreference,
std::upper_bound— forstd::upper_boundbeing a function template defined in<algorithm>that finds the first element greater than the given value using binary search. - cppreference,
std::binary_search— forstd::binary_searchbeing a function template defined in<algorithm>that reports whether an element equivalent to the given value exists in the range. - cppreference,
std::equal_range— forstd::equal_rangebeing a function template defined in<algorithm>that finds the range of elements matching the given value using binary search.
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.