C++
'std::search_n' Is Not a Member of 'std' Fix
std::search_n lives in one header, <algorithm> - and in the sweep probed here, covering all 106 standard headers in this library's header set, only 5 other headers pull it in on the side, so the textbook <vector> program fails outright. This name is also the family member with the barest diagnostic: in all 100 failing sole-header contexts the error arrives with no 'did you mean' tail at all, so the reader gets the plain line and nothing else. And the call itself searches for a run - a count of consecutive equal elements - returning where the run starts or the end when no run is long enough, which makes its count argument, including the non-positive counts that return the beginning here, part of the error's fix story. This guide pins down the failing shapes, the missing suggestion, and the include that fixes the case by rule.
Written December 3, 2026 · Tested with GCC 13.3.0 · 27 min read
- The error: the include is missing
- The textbook version fails too - with the same plain line
- Neighboring headers: 100 fail plainly, and 5 rescue it by accident
- The fix
Written December 3, 2026
Tested with GCC 13.3.0
What this guide is
std::search_n is a function template defined in the header <algorithm>. It searches a range for the first run of count consecutive elements equal to a value, and returns an iterator to the run's first element: over 5, 7, 7, 7, 2, 7, a count of 3 against the value 7 returns the iterator at position 1. When no run is long enough, it returns the range's end. A second form takes a binary predicate and treats an element as matching when the predicate holds between the element and the value. The range needs forward iterators. The documented cost is at most one comparison per element. The classic call dates to C++98, has an execution-policy overload since C++17, and has been usable in constant expressions since C++20.
Because the function lives in <algorithm> - not in a container's header - 'search_n' is not a member of 'std' is the missing include, and naming that cause is the first step:
- 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, that second clause rarely saves anyone: in the sweep below, only 5 of the other 105 headers in the library's header set pull the name in on the side, so most files without<algorithm>do reach this error.
For this name the missing include is also the only cause of the error. std::search_n starts at C++98, so no standard setting produces this wording for the classic call: a file compiled as C++98 with <algorithm> included compiles and runs, as the versions section below shows. The pieces layered on top still have floors of their own - the execution-policy overload needs C++17 and constant-expression use needs C++20 - and those floors belong to the added pieces, not to the classic call.
This name's diagnostic is the family's barest one. In the sweep below, all 100 failing sole-header contexts produce the plain error line alone - no did you mean tail in any of them, no note lines, and no help lines. Where its siblings' errors at least offer a spelling guess to ignore, this error offers nothing: the reader learns that the name is not a member of std in this file, and the declaring header has to come from knowledge, not from the diagnostic. The sweep covers every one of the 106 standard headers in this library's header set, compiled as the only header in the program, and the rescue set has only 6 entries, because in this library the name is declared by a header that very little of the library includes.
The error: the include is missing
This program searches 5, 7, 7, 7, 2, 7 for a run of 3 consecutive values equal to 7, but includes only <limits>:
#include <limits>
int main() {
int a[6] = {5, 7, 7, 7, 2, 7};
int* p = std::search_n(a, a + 6, 3, 7);
return 0;
}
-std=c++20 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:4:19: error: 'search_n' is not a member of 'std'
4 | int* p = std::search_n(a, a + 6, 3, 7);
| ^~~~~~~~
Read it in full: one plain error line - no suggestion tail rides on it, there are no note lines, and there are no help lines. Nothing in the diagnostic names <algorithm> or points anywhere at all. The same program with no include at all fails the same way:
-std=c++20 n_none.cpp -o n_none_prog
n_none.cpp: In function 'int main()':
n_none.cpp:3:19: error: 'search_n' is not a member of 'std'
3 | int* p = std::search_n(a, a + 6, 3, 7);
| ^~~~~~~~
The textbook version fails too - with the same plain line
The version most often written first - search a vector for a run of equal values - produces an error in this library, without <algorithm>, and the error is the same bare line:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 7, 7, 7, 2, 7};
auto p = std::search_n(v.begin(), v.end(), 3, 7);
std::cout << (p - v.begin()) << "\n";
return 0;
}
-std=c++20 t_textbook.cpp -o t_textbook_prog
t_textbook.cpp: In function 'int main()':
t_textbook.cpp:5:19: error: 'search_n' is not a member of 'std'
5 | auto p = std::search_n(v.begin(), v.end(), 3, 7);
| ^~~~~~~~
Nothing rescues the call here: <vector> does not pull the algorithm in - in the sweep below it fails for this name, with this same plain line. And there is no suggestion to follow or to ignore: this diagnostic names no other name at all. A program that uses std::search_n includes <algorithm>, whether or not it happens to compile without it. A sole <algorithm> file makes the point in miniature - the call compiles and runs with nothing else in sight:
-std=c++20 i_algorithm.cpp -o i_algorithm_prog
$ ./i_algorithm_prog
Neighboring headers: 100 fail plainly, and 5 rescue it by accident
In a sweep where each of the 106 standard headers in this library's header set was compiled as the only header in a program that searches 5, 7, 7, 7, 2, 7 for a run of 3 consecutive values equal to 7 and requires the run back at position 1, the outcome splits two ways - the plain failure, or a rescue. Every diagnostic component was classified separately - the error line, any suggestion tail on it, any note lines, and any help lines. These 100 headers all fail with the plain is not a member of 'std' error line - no suggestion tail on that line, no note lines, and no help lines:
<any><array><atomic><barrier><bit><bitset><cassert><ccomplex><cctype><cerrno><cfenv><cfloat><charconv><cinttypes><ciso646><climits><clocale><cmath><codecvt><compare><complex><concepts><condition_variable><coroutine><csetjmp><csignal><cstdalign><cstdarg><cstdbool><cstddef><cstdint><cstdio><cstdlib><cstring><ctgmath><ctime><cuchar><cwchar><cwctype><deque><exception><expected><filesystem><format><forward_list><fstream><future><initializer_list><iomanip><ios><iosfwd><iostream><istream><iterator><latch><limits><list><locale><map><memory><memory_resource><mutex><new><numbers><numeric><optional><ostream><queue><random><ranges><ratio><scoped_allocator><semaphore><set><shared_mutex><source_location><span><spanstream><sstream><stack><stacktrace><stdexcept><stdfloat><stop_token><streambuf><string><string_view><syncstream><system_error><thread><tuple><type_traits><typeindex><typeinfo><unordered_map><unordered_set><utility><variant><vector><version>
A file with no include at all fails the plain way. Two entries deserve a pause. <limits> fails plainly: the header of the numeric limits declares nothing close enough to this name to trigger a guess, which is why the first section's program shows the bare line. <ranges> fails plainly for the classic name: the C++20 ranges library has its own search-n name, and it does not bring this one in - that name gets its own section below. And <vector> - the header of the container this call most often searches - fails plainly, as the textbook section above shows.
No failing header in any shape produced a suggestion, a help line, or a different error.
And these headers rescue the program - it compiles and runs with no <algorithm> anywhere in the file:
<algorithm><chrono><execution><functional><regex><valarray>
The declaring header in this library sits underneath very little - a time header, a policy header, a function-object header, the regular-expression header, and the numeric-array header - so the rescue set runs to only 5 transitive entries. That is also why the failing set is nearly the whole library here: with 100 of the other 105 headers failing, the missing include shows itself in almost any file that does not name <algorithm>. <algorithm> - the home header - is on the rescue list by definition, not by transitivity, and the other 5 rescues are 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 = {5, 7, 7, 7, 2, 7};
auto p = std::search_n(v.begin(), v.end(), 3, 7);
std::cout << "found " << (p - v.begin()) << " value " << *p << "\n";
auto q = std::search_n(v.begin(), v.end(), 4, 7);
std::cout << "four " << (q == v.end() ? "end" : "found") << "\n";
return 0;
}
-std=c++20 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
found 1 value 7
four end
One include, <algorithm>, declares the function - and the fixed program prints two lines that carry this name's shape. The first, found 1 value 7, is the successful search: the run of 3 consecutive values equal to 7 starts at position 1, and the returned iterator points at the run's first element. The second, four end, is the failed search stated the same way: a count of 4 asks for a run one element longer than the run the data holds, and the call answers with the end iterator. The name is now declared by a header the file names itself, instead of by an internal include of one of the 5 rescuing headers that a different library version is free to rearrange. For a file that does fail - the <limits> shape above, or any other - the same one include is the whole fix for the error, at every standard level: this name has no floor case, as the versions section below shows.
What the call does: it finds the first run of count consecutive equal elements
The call's whole result is a position - where a run starts, or the end when no run qualifies - and its defining choices are the count's role and the boundaries. The count is a length, not a value: a run qualifies when at least count consecutive elements equal the value, the answer is the run's first element, and a non-positive count is a boundary of its own in this library. This program walks the shapes in order:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 7, 7, 7, 2, 7};
auto p = std::search_n(v.begin(), v.end(), 3, 7);
std::cout << "run3 " << (p - v.begin()) << "\n";
auto p2 = std::search_n(v.begin(), v.end(), 2, 7);
std::cout << "run2 " << (p2 - v.begin()) << "\n";
auto p4 = std::search_n(v.begin(), v.end(), 4, 7);
std::cout << "run4 " << (p4 == v.end() ? "end" : "found") << "\n";
std::vector<int> w = {7, 7, 7, 7};
auto pw = std::search_n(w.begin(), w.end(), 3, 7);
std::cout << "overlap " << (pw - w.begin()) << "\n";
std::vector<int> x = {1, 2, 9, 9, 9};
auto px = std::search_n(x.begin(), x.end(), 3, 9);
std::cout << "tail " << (px - x.begin()) << "\n";
auto p0 = std::search_n(v.begin(), v.end(), 0, 7);
std::cout << "zero " << (p0 - v.begin()) << " " << (p0 == v.end() ? "end" : "mid") << "\n";
auto pn = std::search_n(v.begin(), v.end(), -1, 7);
std::cout << "neg " << (pn - v.begin()) << " " << (pn == v.end() ? "end" : "mid") << "\n";
std::vector<int> empty;
auto pe = std::search_n(empty.begin(), empty.end(), 1, 7);
std::cout << "empty " << (pe == empty.end() ? "end" : "mid") << "\n";
auto pp = std::search_n(v.begin(), v.end(), 3, 0, [](int e, int) { return e > 4; });
std::cout << "pred " << (pp - v.begin()) << "\n";
return 0;
}
-std=c++20 r_semantics.cpp -o r_semantics_prog
$ ./r_semantics_prog
run3 1
run2 1
run4 end
overlap 0
tail 2
zero 0 mid
neg 0 mid
empty end
pred 0
Read in order: the run3 line reports 1 - over 5, 7, 7, 7, 2, 7, the first run of 3 consecutive values equal to 7 starts at position 1. The run2 line also reports 1: a count of 2 is satisfied by the same run, because the call returns the run's start, not its end - the count changes which runs qualify, not where a qualifying run begins. The run4 line reports end: no 4 consecutive elements equal 7, so the call returns the end iterator. The overlap line reports 0: over 7, 7, 7, 7, a count of 3 is satisfied at the very first position - the run is longer than the count, and the answer is still its start. The tail line reports 2: the run of three 9 values at the end of 1, 2, 9, 9, 9 starts at position 2, and a run that ends exactly at the range's end still counts. The zero line reports 0 mid and the neg line reports 0 mid: in this library a count of 0 and a count of -1 both return the beginning of the range - the position-0 iterator, not the end - matching the shape of the reference implementation cppreference prints, which returns the first iterator when the count is not positive. The empty line reports end: over an empty range the returned iterator is the end itself. And the pred line reports 0: the predicate form, testing each element against a dummy value with a predicate that holds when the element is greater than 4, finds the run 5, 7, 7, 7 - three consecutive elements greater than 4 - starting at position 0.
The cost rule is the documented bound: with a range of length N, the call makes at most N comparisons - one pass over the range, however long the runs are.
Versions: no floor for the classic call - it dates to C++98
This call has no floor inside the probed set. std::search_n dates to C++98 - the cppreference page carries no since marker on the classic overloads, and its defect reports are applied to C++98, which only makes sense for a name that already existed there. The fixed programs in this guide are compiled as C++20, but the name is far older than that: with <algorithm> included, the same program compiled as C++98 compiles and runs, printing 1 7 - the run's position and the value standing there - and as C++11, C++14, C++17, C++20, and C++23 the same way:
#include <algorithm>
#include <iostream>
int main() {
int a[6] = {5, 7, 7, 7, 2, 7};
int* p = std::search_n(a, a + 6, 3, 7);
std::cout << (p - a) << " " << *p << "\n";
return 0;
}
-std=c++98 c_version98.cpp -o c_version98_prog
$ ./c_version98_prog
1 7
So this name's error has one cause, not two: a missing <algorithm>. A file that includes <algorithm> and still sees this error does not have a standard problem; the include is not reaching the file that names the call, or the name in the file is not this name. No standard upgrade fixes a missing include, and no include change is needed for an old standard - the two halves of the family's usual advice separate cleanly for this name.
The pieces layered on top of the classic call carry their own floors. This name does have an execution-policy overload, since C++17 - and naming the policy with only <algorithm> included fails on the policy's name, not the algorithm's:
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v = {5, 7, 7, 7, 2, 7};
auto p = std::search_n(std::execution::seq, v.begin(), v.end(), 3, 7);
return (p - v.begin() == 1) ? 0 : 1;
}
-std=c++20 p_policy_algonly.cpp -o p_policy_algonly_prog
p_policy_algonly.cpp: In function 'int main()':
p_policy_algonly.cpp:5:33: error: 'std::execution' has not been declared
5 | auto p = std::search_n(std::execution::seq, v.begin(), v.end(), 3, 7);
| ^~~~~~~~~
That wording - has not been declared, naming std::execution - is the missing-header shape, and here adding the header is the whole fix: add <execution> alongside <algorithm>, and the same call compiles and runs, because the policy overload of std::search_n exists - and the policy form of this name returns the same run-start iterator as the classic call:
-std=c++20 p_policy_both.cpp -o p_policy_both_prog
$ ./p_policy_both_prog
The policy form belongs to this name, and it carries the C++17 floor. Compiled as C++14, the same two-header program fails on the policy's name again - at that standard the policy names are not there to be declared:
-std=c++14 p_policy_cpp14.cpp -o p_policy_cpp14_prog
p_policy_cpp14.cpp: In function 'int main()':
p_policy_cpp14.cpp:6:33: error: 'std::execution' has not been declared
6 | auto p = std::search_n(std::execution::seq, v.begin(), v.end(), 3, 7);
| ^~~~~~~~~
And since C++20 the classic call is also usable in constant expressions - this program's check happens at compile time, and the assertion on the result is part of the build:
#include <algorithm>
#include <array>
constexpr int check() {
std::array<int, 6> a{5, 7, 7, 7, 2, 7};
auto p = std::search_n(a.begin(), a.end(), 3, 7);
return (p - a.begin() == 1) ? 1 : 0;
}
static_assert(check() == 1);
int main() { return 0; }
-std=c++20 c_constexpr20.cpp -o c_constexpr20_prog
$ ./c_constexpr20_prog
Compiled as C++17, the same program fails - the call is not a constant expression there, and the static assertion cannot hold:
-std=c++17 c_constexpr17.cpp -o c_constexpr17_prog
c_constexpr17.cpp: In function 'constexpr int check()':
c_constexpr17.cpp:5:27: warning: call to non-'constexpr' function '_FIter std::search_n(_FIter, _FIter, _Size, const _Tp&) [with _FIter = int*; _Size = int; _Tp = int]' [-Winvalid-constexpr]
5 | auto p = std::search_n(a.begin(), a.end(), 3, 7);
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/13/algorithm:61,
from c_constexpr17.cpp:1:
/usr/include/c++/13/bits/stl_algo.h:4213:5: note: '_FIter std::search_n(_FIter, _FIter, _Size, const _Tp&) [with _FIter = int*; _Size = int; _Tp = int]' declared here
4213 | search_n(_ForwardIterator __first, _ForwardIterator __last,
| ^~~~~~~~
c_constexpr17.cpp: At global scope:
c_constexpr17.cpp:8:23: error: non-constant condition for static assertion
8 | static_assert(check() == 1);
| ~~~~~~~~^~~~
c_constexpr17.cpp:8:20: error: 'constexpr int check()' called in a constant expression
8 | static_assert(check() == 1);
| ~~~~~^~
c_constexpr17.cpp:3:15: note: 'constexpr int check()' declared here
3 | constexpr int check() {
| ^~~~~
So C++17 is the floor for the policy form and C++20 is the floor for constant-expression use - and no standard at any level rescues a missing include, while the classic call needs nothing beyond <algorithm> at any standard from C++98 up.
The neighboring names: seven fail, one compiles outright
The names a reader most often reaches for alongside this one mostly live in <algorithm> too, and in a file whose only include is <vector> this family splits - seven of the eight names fail, and one compiles. std::find, which returns an iterator to the first element equal to a value, fails with a suggestion that points into the ranges library:
-std=c++20 b_find_vector.cpp -o b_find_vector_prog
b_find_vector.cpp: In function 'int main()':
b_find_vector.cpp:5:20: error: 'find' is not a member of 'std'; did you mean 'std::ranges::find'?
5 | auto it = std::find(v.begin(), v.end(), 3);
| ^~~~
In file included from /usr/include/c++/13/tuple:44,
from /usr/include/c++/13/bits/uses_allocator_args.h:38,
from /usr/include/c++/13/bits/memory_resource.h:41,
from /usr/include/c++/13/vector:80,
from b_find_vector.cpp:1:
/usr/include/c++/13/bits/ranges_util.h:499:30: note: 'std::ranges::find' declared here
499 | inline constexpr __find_fn find{};
| ^~~~
std::copy, which copies a range into another range, is the split: in this library a sole <vector> file compiles it outright, with no <algorithm> anywhere - the container header pulls the name in on the side here, as this library version's behavior, not as a rule:
-std=c++20 b_copy_vector.cpp -o b_copy_vector_prog
$ ./b_copy_vector_prog
std::transform, which applies a function to a range and stores the results in a destination range, fails with the plain error:
-std=c++20 b_transform_vector.cpp -o b_transform_vector_prog
b_transform_vector.cpp: In function 'int main()':
b_transform_vector.cpp:5:20: error: 'transform' is not a member of 'std'
5 | auto it = std::transform(v.begin(), v.end(), dest.begin(), [](int n) { return n * 2; });
| ^~~~~~~~~
std::count, which counts the elements equal to a value, fails with a suggestion that points at a bit-counting function, not at any algorithm:
-std=c++20 b_count_vector.cpp -o b_count_vector_prog
b_count_vector.cpp: In function 'int main()':
b_count_vector.cpp:5:19: error: 'count' is not a member of 'std'; did you mean 'popcount'?
5 | auto c = std::count(v.begin(), v.end(), 3);
| ^~~~~
| popcount
std::sort, which orders a range in place, fails with the plain error:
-std=c++20 b_sort_vector.cpp -o b_sort_vector_prog
b_sort_vector.cpp: In function 'int main()':
b_sort_vector.cpp:5:10: error: 'sort' is not a member of 'std'
5 | std::sort(v.begin(), v.end());
| ^~~~
std::remove, which removes the elements equal to a value by compacting the kept elements forward, fails with a suggestion that points at a different algorithm altogether:
-std=c++20 b_remove_vector.cpp -o b_remove_vector_prog
b_remove_vector.cpp: In function 'int main()':
b_remove_vector.cpp:5:20: error: 'remove' is not a member of 'std'; did you mean 'move'?
5 | auto it = std::remove(v.begin(), v.end(), 3);
| ^~~~~~
| move
std::unique, which compacts away consecutive duplicate elements and returns a new logical end, fails with the plain error:
-std=c++20 b_unique_vector.cpp -o b_unique_vector_prog
b_unique_vector.cpp: In function 'int main()':
b_unique_vector.cpp:5:20: error: 'unique' is not a member of 'std'
5 | auto it = std::unique(v.begin(), v.end());
| ^~~~~~
While std::partition, which rearranges a range so the elements a predicate matches come first and returns the partition point, fails with the plain error too:
-std=c++20 b_partition_vector.cpp -o b_partition_vector_prog
b_partition_vector.cpp: In function 'int main()':
b_partition_vector.cpp:5:20: error: 'partition' is not a member of 'std'
5 | auto it = std::partition(v.begin(), v.end(), [](int n) { return n % 2 == 0; });
| ^~~~~~~~~
This name itself never earns a suggestion in any probed context, but its neighbors do: one names the ranges form of a neighbor, one names a bit-counting function, and one names a moving algorithm - all three are spelling guesses, and none of them does what the failed name does. And the one name that compiles, std::copy, compiles by accident of this library's internal includes - the rule for it is the same <algorithm> as for the rest. The failing neighbors are shown here only as neighbors, not re-taught.
The neighboring names, fixed
Together the nine names take one header between them - <algorithm> for all nine, with <vector> covering only the containers they work on. The fixed neighbor program reports all nine operations:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 7, 7, 7, 2, 7};
auto sf = std::search_n(v.begin(), v.end(), 3, 7);
std::cout << "search_n " << (sf - v.begin()) << " " << *sf << "\n";
std::vector<int> u = {1, 2, 3, 4, 5, 6};
auto f = std::find(u.begin(), u.end(), 3);
std::cout << "find " << (f - u.begin()) << " " << *f << "\n";
auto c = std::count(u.begin(), u.end(), 3);
std::cout << "count " << c << "\n";
std::vector<int> dest(6);
std::copy(u.begin(), u.end(), dest.begin());
std::cout << "copy " << dest.front() << " " << dest.back() << "\n";
std::vector<int> doubled(6);
std::transform(u.begin(), u.end(), doubled.begin(), [](int n) { return n * 2; });
std::cout << "transform";
for (int n : doubled) std::cout << " " << n;
std::cout << "\n";
std::vector<int> w = {6, 3, 5, 1, 4, 2};
std::sort(w.begin(), w.end());
std::cout << "sort";
for (int n : w) std::cout << " " << n;
std::cout << "\n";
std::vector<int> r = {1, 2, 3, 4, 3, 6};
auto re = std::remove(r.begin(), r.end(), 3);
std::cout << "remove";
for (auto it = r.begin(); it != re; ++it) std::cout << " " << *it;
std::cout << " end " << (re - r.begin()) << "\n";
std::vector<int> q = {1, 1, 2, 2, 3, 3};
auto qe = std::unique(q.begin(), q.end());
std::cout << "unique";
for (auto it = q.begin(); it != qe; ++it) std::cout << " " << *it;
std::cout << " end " << (qe - q.begin()) << "\n";
std::vector<int> p = {1, 2, 3, 4, 5, 6};
auto pp = std::partition(p.begin(), p.end(), [](int n) { return n % 2 == 0; });
std::cout << "partition end " << (pp - p.begin()) << " first";
for (auto it = p.begin(); it != pp; ++it) std::cout << " " << *it;
std::cout << "\n";
return 0;
}
-std=c++20 b_neighbors_fixed.cpp -o b_neighbors_fixed_prog
$ ./b_neighbors_fixed_prog
search_n 1 7
find 2 3
count 1
copy 1 6
transform 2 4 6 8 10 12
sort 1 2 3 4 5 6
remove 1 2 4 6 end 4
unique 1 2 3 end 3
partition end 3 first 6 2 4
Read in order: search_n reports 1 7 - the run of 3 consecutive values equal to 7 starts at position 1, and the value standing there is 7. find reports position 2 and the value 3 - the first element equal to 3. count reports 1 - one element equals 3. copy reports 1 6 - the destination's first and last elements after the range was copied into it. transform reports 2 4 6 8 10 12 - each element doubled into the destination. sort reports 1 2 3 4 5 6 - the shuffled range 6, 3, 5, 1, 4, 2 put back in order. remove reports 1 2 4 6 end 4 - both elements equal to 3 compacted out of 1, 2, 3, 4, 3, 6, with the new end at position 4. unique reports 1 2 3 end 3 - the consecutive duplicates in 1, 1, 2, 2, 3, 3 compacted away. And partition reports end 3 with the first segment holding the three even values - partition makes no promise about the order inside either segment, so the segment is reported as it came out, not as a sorted result.
A ranges form since C++20, in the same header
C++20 added a ranges form of this name, and the standard declares it in the same header as the classic name. std::ranges::search_n is a function object in the ranges namespace, declared in <algorithm>, that accepts a whole range - or an iterator pair - and returns a subrange covering exactly the found run: its beginning is the run's first element and its end stands one past the run's last element, so its size is the count when a run is found, and it is empty when no run qualifies. Compiled as C++20 it answers for the same vector:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 7, 7, 7, 2, 7};
auto r = std::ranges::search_n(v, 3, 7);
std::cout << "ranges " << (r.begin() - v.begin()) << " " << (r.end() - v.begin()) << " size " << r.size() << "\n";
auto r4 = std::ranges::search_n(v, 4, 7);
std::cout << "ranges4 " << (r4.empty() ? "empty" : "found") << "\n";
return 0;
}
-std=c++20 r_ranges.cpp -o r_ranges_prog
$ ./r_ranges_prog
ranges 1 4 size 3
ranges4 empty
The ranges line reports 1 4 size 3 - the found run of 3 consecutive values equal to 7 occupies positions 1 through 3, so the subrange begins at 1, ends at 4, and holds 3 elements. The ranges4 line reports empty: a count of 4 finds no run, and the returned subrange is empty rather than an end iterator - the ranges form states a failed search as an empty result, where the classic call states it as the end. Two boundaries keep the forms straight. The ranges form needs C++20: compiled as C++17, that same program fails on the namespace itself, with 'std::ranges' has not been declared:
-std=c++17 r_ranges_cpp17.cpp -o r_ranges_cpp17_prog
r_ranges_cpp17.cpp: In function 'int main()':
r_ranges_cpp17.cpp:6:19: error: 'std::ranges' has not been declared
6 | auto r = std::ranges::search_n(v, 3, 7);
| ^~~~~~
r_ranges_cpp17.cpp:8:20: error: 'std::ranges' has not been declared
8 | auto r4 = std::ranges::search_n(v, 4, 7);
| ^~~~~~
And in this library the ranges header does not rescue the ranges form - a file whose only include is <ranges> (with <vector> for the containers) fails for it with a not-a-member error naming the ranges namespace, and a file whose only include is <array> fails for it too, with one difference between the two transcripts: the <ranges> file's error carries the suggestion tail did you mean 'search'? - the one suggestion this name earns in any probed context, pointing at the ranges library's own search algorithm - while the <array> file's error is the plain line alone:
-std=c++20 r_ranges_rangesonly.cpp -o r_ranges_rangesonly_prog
r_ranges_rangesonly.cpp: In function 'int main()':
r_ranges_rangesonly.cpp:5:27: error: 'search_n' is not a member of 'std::ranges'; did you mean 'search'?
5 | auto r = std::ranges::search_n(v, 3, 7);
| ^~~~~~~~
| search
-std=c++20 r_ranges_arrayonly.cpp -o r_ranges_arrayonly_prog
r_ranges_arrayonly.cpp: In function 'int main()':
r_ranges_arrayonly.cpp:4:27: error: 'search_n' is not a member of 'std::ranges'
4 | auto r = std::ranges::search_n(v, 3, 7);
| ^~~~~~~~
That is this library version's behavior, not the rule: the header the standard names for both forms is <algorithm>, so a complete program names it. The classic name, meanwhile, is not pulled in by the ranges header either: in the sweep probed here, a file whose only include is <ranges> fails for std::search_n with the plain error line, as the sweep section above lists.
A different wording - and an argument-dependent rescue that depends on the iterators' types
Including <algorithm> is not the only half of the name. Over plain arrays, the unqualified call fails:
#include <algorithm>
int main() {
int a[6] = {5, 7, 7, 7, 2, 7};
int* p = search_n(a, a + 6, 3, 7);
return 0;
}
-std=c++20 g_unqualified.cpp -o g_unqualified_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:4:14: error: 'search_n' was not declared in this scope
4 | int* p = search_n(a, a + 6, 3, 7);
| ^~~~~~~~
g_unqualified.cpp:4:14: note: suggested alternatives:
In file included from /usr/include/c++/13/algorithm:73,
from g_unqualified.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:127:1: note: 'std::search_n'
127 | search_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Size __count,
| ^~~~~~~~
In file included from /usr/include/c++/13/algorithm:63:
/usr/include/c++/13/bits/ranges_algo.h:433:34: note: 'std::ranges::search_n'
433 | inline constexpr __search_n_fn search_n{};
| ^~~~~~~~
That wording - was not declared in this scope, with notes pointing at the declarations of both std::search_n and the ranges form - is a different defect with a different fix: the header is present, the qualifier is not. Qualify the name as std::search_n; adding includes does not bring the name out of namespace std. Swap the arrays for vectors, and the same unqualified call compiles and runs:
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v = {5, 7, 7, 7, 2, 7};
auto p = search_n(v.begin(), v.end(), 3, 7);
return (p - v.begin() == 1) ? 0 : 1;
}
-std=c++20 g_unqualified_vec.cpp -o g_unqualified_vec_prog
$ ./g_unqualified_vec_prog
That compile is argument-dependent lookup at work, not a rule to rely on: the vectors' iterators are std types, so their associated namespace is std, and lookup finds std::search_n through them. A list's iterators rescue it the same way - the same unqualified call over lists compiles and runs:
-std=c++20 g_unqualified_list.cpp -o g_unqualified_list_prog
$ ./g_unqualified_list_prog
But the container being a std type is not enough by itself - over a std::array, the same unqualified call fails with the same wording, because in this library a std::array's iterators are plain pointers, and a pointer has no associated namespace:
#include <algorithm>
#include <array>
int main() {
std::array<int, 6> v{5, 7, 7, 7, 2, 7};
auto p = search_n(v.begin(), v.end(), 3, 7);
return 0;
}
-std=c++20 g_unqualified_stdarray.cpp -o g_unqualified_stdarray_prog
g_unqualified_stdarray.cpp: In function 'int main()':
g_unqualified_stdarray.cpp:5:14: error: 'search_n' was not declared in this scope
5 | auto p = search_n(v.begin(), v.end(), 3, 7);
| ^~~~~~~~
g_unqualified_stdarray.cpp:5:14: note: suggested alternatives:
In file included from /usr/include/c++/13/algorithm:73,
from g_unqualified_stdarray.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:127:1: note: 'std::search_n'
127 | search_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Size __count,
| ^~~~~~~~
In file included from /usr/include/c++/13/algorithm:63:
/usr/include/c++/13/bits/ranges_algo.h:433:34: note: 'std::ranges::search_n'
433 | inline constexpr __search_n_fn search_n{};
| ^~~~~~~~
And with using namespace std; in the file, the qualifier question goes away by a different route - the unqualified call over vectors compiles and runs, because the using-directive brings the name into scope directly:
-std=c++20 g_using_namespace.cpp -o g_using_namespace_prog
$ ./g_using_namespace_prog
The pattern across the probed set is exact: the rescue follows the iterator type, not the container's name - class iterators of std containers carry the name in, pointer iterators do not, whatever container stands behind them - and a using-directive bypasses the question entirely. Write the qualifier either way.
The exact rule
std::search_nis declared by#include <algorithm>, and by no other header by rule. In the sweep probed here, 100 of the other 105 headers in the library's header set - among them<limits>,<ranges>,<vector>,<filesystem>, and<string>- do not declare it, and neither does a file with no include at all; the remaining 5 pull it in transitively, as this library version's behavior, not as a guarantee.'search_n' is not a member of 'std'has one cause in the contexts probed here: the<algorithm>include is missing and nothing else in the file pulled the name in. It comes in one shape in the sweep probed here: all 100 failing sole headers fail with the plain error line alone - no suggestion tail, no note lines, no help lines, and no different error from any header. And unlike its younger siblings, this name has no standard-based second cause: the classic call dates to C++98 and compiles with the include in place at every probed standard.- In the sweep probed here, 5 headers besides
<algorithm>rescuestd::search_ntransitively -<chrono>,<execution>,<functional>,<regex>, and<valarray>- 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, no header besides<algorithm>is required to pullstd::search_nin. - The result rules are the documented ones, as the fixed programs here show: the fixed program prints
found 1 value 7andfour endfor the textbook case; the semantics program printsrun3 1,run2 1,run4 end,overlap 0,tail 2,zero 0 mid,neg 0 mid,empty end, andpred 0- the call returns the first element of the first run ofcountconsecutive elements equal to the value, returns the end when no run is long enough or the range is empty, returns the beginning for a non-positive count in this library, and makes at most one comparison per element. - The classic
std::search_ndates to C++98: with<algorithm>included it compiles and runs as C++98, C++11, C++14, C++17, C++20, and C++23, each printing1 7. The layered pieces have floors: the execution-policy overload starts at C++17 - naming the policy with only<algorithm>included fails with'std::execution' has not been declared; with<execution>included alongside it, the policy call compiles and runs, returning the run's start. Constant-expression use starts at C++20: the static-assert program here compiles as C++20 and fails as C++17. - The neighbors split in this library:
std::findfails in a sole<vector>file with the suggestiondid you mean 'std::ranges::find'?,std::countwithdid you mean 'popcount'?, andstd::removewithdid you mean 'move'?, whilestd::transform,std::sort,std::unique, andstd::partitionfail there with the plain error, andstd::copycompiles outright in that file - all eight neighbors are declared by#include <algorithm>by rule. The fixed neighbor program printssearch_n 1 7,find 2 3,count 1,copy 1 6,transform 2 4 6 8 10 12,sort 1 2 3 4 5 6,remove 1 2 4 6 end 4,unique 1 2 3 end 3, andpartition end 3. - The ranges form of this same name is a function object in namespace
std::ranges, declared in<algorithm>by the standard, needing C++20, returning a subrange covering exactly the found run - compiled as C++17 the ranges program here fails with'std::ranges' has not been declared, and compiled as C++20 it printsranges 1 4 size 3andranges4 empty. In the probes here<ranges>alone does not rescue it in this library - it fails with a not-a-member error naming therangesnamespace for the ranges form,<array>alone fails for it with the plain line while the<ranges>transcript adds adid you mean 'search'?tail, and<ranges>fails with the classic name's plain error for the classic name in the sweep. 'search_n' was not declared in this scopefor an unqualifiedsearch_nuse is a different defect: qualify the name asstd::search_n- the include alone does not bring the name out of namespacestd. For this name the unqualified call's fate depends on the iterators' types: over plain arrays it fails, as the first unqualified program above shows; over a vector's iterators and over a list's iterators it is rescued through argument-dependent lookup, as the second and third show, because those iterators arestdclass types; over astd::array's iterators it fails again, because in this library those iterators are plain pointers; and withusing namespace std;in the file it compiles outright, as the last unqualified program shows. Qualify the name either way.
The fixes, ranked
- First, check the include list of the file that names
std::search_n. If<algorithm>is not there, add it - that is the fix for the only cause of this error. It holds whether or not the file happened to compile before through one of the 5 transitive rescues and stopped compiling when its other includes changed - all 100 of the probed failures are this one cause. - Second, do not wait for the diagnostic to name the header: this error carries no suggestion in any probed context, so there is no
did you meantail to follow or to rule out - the declaring header comes from the rule, not from the error text. A suggestion on a neighboring name's error is likewise a spelling guess, never a fix. - Third, do not chase a standard upgrade for this error: this name dates to C++98, so a build set to any standard from C++98 up keeps the classic call with the include in place, and the error with the include present is never a standard problem.
- Fourth, do not reach for a container's header because the call searches a container, or for
<iterator>because the call takes iterators. In the sweep probed here<vector>,<list>,<string>, and<iterator>all fail for this name - none of them is the declaring header by rule. The declaring header is<algorithm>, and a complete program names it alongside the container's own header. - Fifth, do not remove
<algorithm>because a file compiles without it: in the probed set only 5 headers rescue the classic name, and those rescues are not guaranteed - a file that drops the include is one internal-include change away from the error in the first section. The same warning coversstd::copy, which compiles in a sole<vector>file here by the same kind of accident. - Sixth, read a returned end iterator as a failed search, not as a found one: the call answers
endwhen no run reaches the count and when the range is empty - in the fixed program the count of 4 answersendagainst a run of 3 - and read a non-positive count's answer in this library as the beginning of the range, not the end: the zero and negative probes here both returned position 0. - Seventh, if the error names
std::executioninstead -'std::execution' has not been declared- add<execution>alongside<algorithm>and keep the standard at C++17 or later: this name does have a policy overload, and that pair of changes is the whole fix for the policy form. - Eighth, match the standard to the one piece beyond the classic call that has a floor: constant-expression use needs C++20, and the ranges form needs C++20 - with its header still
<algorithm>by the standard, not<ranges>. - Ninth, if the error says
was not declared in this scopeinstead, fix the name's qualifier tostd::search_nas well - and do not rely on argument-dependent lookup to rescue the unqualified call, because a plain array and astd::arrayeach remove that rescue in the probed set.
Related guides on this site
- 'std::search' Is Not a Member of 'std' Fix
- 'std::binary_search' Is Not a Member of 'std' Fix
- 'std::unique' Is Not a Member of 'std' Fix
- 'std::replace' Is Not a Member of 'std' Fix
Sources
- cppreference, std::search_n - the function template is defined in
<algorithm>; it searches a range for the first occurrence ofcountconsecutive elements equal to a value, or equal under a binary predicate; a positive count returns the run's beginning, and the documented complexity is at most one comparison per element; the execution-policy overloads are marked since C++17, the classic overloads constexpr since C++20, and the defect reports on the page are applied to C++98. - cppreference,
<algorithm>header - the header that declares this name and the other algorithm names used as neighbors here. - cppreference, std::ranges::search_n - the C++20 ranges form of this name, a function object in namespace
std::rangesdeclared in<algorithm>, returning a subrange covering the found run.
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.