C++

'std::search' Is Not a Member of 'std' Fix

std::search lives in one header, <algorithm> - not in <vector> or <string>, the headers that hold the ranges it searches. On this library version a handful of headers rescue it by accident: in the sweep probed here, covering all 106 standard headers in the library's header set, <execution>, <functional>, <regex>, and <valarray> pull the name in on the side. The error itself comes in two shapes on this library: a plain line with nothing after it, and the same line with a suggestion tail naming std::bsearch, a different function that is not a fix. This guide pins down both failing shapes, the four transitive rescues, and the one include that fixes the error by specification.

Written December 2, 2026 · Tested with GCC 13.3.0 · 18 min read

By the end of this guide
  • The error: the include is missing, in a lean file
  • The textbook version fails too - with a misleading suggestion
  • Neighboring headers: 79 fail plain, 22 fail with the bsearch suggestion, and four rescue it
  • The fix

Written December 2, 2026

Tested with GCC 13.3.0

What this guide is

std::search is a function template defined in the header <algorithm>. It searches for the first occurrence of a target range inside a source range: the four-iterator overload takes the source range and the target range, compares elements with operator== (or with a given binary predicate), and returns an iterator to the beginning of the first occurrence. If the target range is empty, the start of the source range is returned; if the target range does not appear, the end of the source range is returned. Both iterator pairs must meet the forward-iterator requirements. The classic overloads are old - they compile under C++98, as the versions section below shows, and they are constexpr since C++20; the execution-policy overloads and the searcher overload were added in C++17. At most N1 times N2 comparisons are made, where N1 and N2 are the lengths of the source and target ranges.

Because the function lives in one specific header, 'search' 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 lines and no help lines; the only variation is a suggestion tail on the error line itself, present for some headers and absent for others, naming bsearch - a different function, not the missing name's home.

What makes this error worth separating from its closest relatives is that suggestion. A reader whose ranges live in <vector> or <string> gets an error that appears to offer a fix - did you mean 'bsearch'? - and following it changes the program's meaning: std::bsearch binary-searches a sorted range for one value, while std::search looks for a whole subsequence, in order, in an unsorted range. 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 outcome splits three ways - a plain failure, a failure with the bsearch suggestion tail, or a rescue - and the rescue set has exactly five entries: <algorithm> itself, <execution>, <functional>, <regex>, and <valarray>.

The error: the include is missing, in a lean file

This program searches a five-element array for the two-element subsequence 3, 4, but includes only <limits>:

C++
#include <limits>
int main() {
    int hay[5] = {1, 2, 3, 4, 5};
    int needle[2] = {3, 4};
    std::search(hay, hay + 5, needle, needle + 2);
    return 0;
}
Terminal
$ g++ -std=c++17 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:5:10: error: 'search' is not a member of 'std'
    5 |     std::search(hay, hay + 5, needle, needle + 2);
      |          ^~~~~~

That is the whole output: one plain error line, and nothing after it. No suggestion tail rides on the error line itself, there are no note lines, and there are no help lines. The same program with no include at all fails the same way, just as bare, at the same call.

The textbook version fails too - with a misleading suggestion

The version most often written first - search a vector for a subsequence - gets the same error, without <algorithm>, but with the suggestion tail attached:

C++
#include <iostream>
#include <vector>
int main() {
    std::vector<int> hay = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::vector<int> needle = {7, 8, 9};
    auto it = std::search(hay.begin(), hay.end(), needle.begin(), needle.end());
    std::cout << (it - hay.begin()) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 t_textbook.cpp -o t_textbook_prog
t_textbook.cpp: In function 'int main()':
t_textbook.cpp:6:20: error: 'search' is not a member of 'std'; did you mean 'bsearch'?
    6 |     auto it = std::search(hay.begin(), hay.end(), needle.begin(), needle.end());
      |                    ^~~~~~
      |                    bsearch

What happened: on this library version, neither <iostream> nor <vector> pulls this name in transitively, so the call fails in a textbook file just as it does in a lean one. The difference is the tail: a header in this file has declared std::bsearch, the C binary-search function, and the compiler offers the nearest declared name. That suggestion is not a fix. std::bsearch takes a single key value and a sorted range and returns a pointer to a matching element; it cannot look for a subsequence, and the ranges here are not being binary-searched. A sole <string> file fails with the same suggestion shape - searching a string for a substring with std::search gets the bsearch tail as well. In the sweep probed here, the only headers that rescue std::search besides <algorithm> itself are <execution>, <functional>, <regex>, and <valarray>, and those rescues are properties of this library version, not rules of the language. A program that uses std::search includes <algorithm>, whether or not it happens to compile without it.

Neighboring headers: 79 fail plain, 22 fail with the bsearch suggestion, and four rescue it

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 a five-element array for a two-element needle, the outcome splits by compile result and by diagnostic shape three ways. Every diagnostic component was classified separately - the error line, any suggestion tail on it, any note lines, and any help lines. These 79 headers all fail with the plain is not a member of 'std' error line, no suggestion tail on that line, no note lines at all, and no help lines:

  • <any>
  • <array>
  • <atomic>
  • <barrier>
  • <bit>
  • <cassert>
  • <cctype>
  • <cerrno>
  • <cfenv>
  • <cfloat>
  • <charconv>
  • <chrono>
  • <cinttypes>
  • <ciso646>
  • <climits>
  • <clocale>
  • <cmath>
  • <compare>
  • <concepts>
  • <condition_variable>
  • <coroutine>
  • <csetjmp>
  • <csignal>
  • <cstdalign>
  • <cstdarg>
  • <cstdbool>
  • <cstddef>
  • <cstdint>
  • <cstdio>
  • <cstring>
  • <ctime>
  • <cuchar>
  • <cwchar>
  • <cwctype>
  • <deque>
  • <exception>
  • <expected>
  • <format>
  • <forward_list>
  • <initializer_list>
  • <iosfwd>
  • <latch>
  • <limits>
  • <list>
  • <map>
  • <memory>
  • <memory_resource>
  • <mutex>
  • <new>
  • <numbers>
  • <numeric>
  • <optional>
  • <queue>
  • <ranges>
  • <ratio>
  • <scoped_allocator>
  • <semaphore>
  • <set>
  • <shared_mutex>
  • <source_location>
  • <span>
  • <spanstream>
  • <stack>
  • <stacktrace>
  • <stdfloat>
  • <stop_token>
  • <string_view>
  • <syncstream>
  • <thread>
  • <tuple>
  • <type_traits>
  • <typeindex>
  • <typeinfo>
  • <unordered_map>
  • <unordered_set>
  • <utility>
  • <variant>
  • <vector>
  • <version>

These 22 headers fail with the same error plus the suggestion tail ; did you mean 'bsearch'? and a bsearch marker line under the caret - still with no note lines and no help lines:

  • <bitset>
  • <ccomplex>
  • <codecvt>
  • <complex>
  • <cstdlib>
  • <ctgmath>
  • <filesystem>
  • <fstream>
  • <future>
  • <iomanip>
  • <ios>
  • <iostream>
  • <istream>
  • <iterator>
  • <locale>
  • <ostream>
  • <random>
  • <sstream>
  • <stdexcept>
  • <streambuf>
  • <string>
  • <system_error>

A file with no include at all fails the plain way, with the same absence of notes. No failing sole header prints a note line or a help line in this sweep. The split between the two failing shapes is which headers have declared std::bsearch by the time the call is reached - the C headers and the stream and string headers are on the suggestion side - and it changes nothing about the fix: both shapes mean the same missing include.

And these headers rescue the program - it compiles and runs with no <algorithm> anywhere in the file:

  • <algorithm>
  • <execution>
  • <functional>
  • <regex>
  • <valarray>

The rescue set is the same five-entry set as the neighboring std::sample sweep: two execution and functional headers, the regular-expression header, and <valarray> - headers that pull the algorithm header's internals in on the side, which is consistent with a name whose home is the general algorithms header. <algorithm> - the home header - is on the rescue list by definition, not by transitivity, and the other four rescues are this library version's behavior, not a guarantee.

The fix

Add the header:

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> hay = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::vector<int> needle = {7, 8, 9};
    auto it = std::search(hay.begin(), hay.end(), needle.begin(), needle.end());
    std::cout << (it - hay.begin()) << ": " << *it << " " << *(it + 1) << " " << *(it + 2) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
6: 7 8 9

One include, <algorithm>, declares the function. The returned iterator sits 6 past the start of the vector - the subsequence 7, 8, 9 starts at index 6 in the population 1 through 10 - and dereferencing it and its two successors gives the found subsequence itself, 7, 8, 9.

The function's return value carries the whole result, and its two other documented cases are just as exact. The return is the first occurrence, not any occurrence, and a miss returns the end of the source range:

C++
#include <algorithm>
#include <iostream>
int main() {
    int hay[6] = {5, 1, 2, 3, 1, 2};
    int needle[2] = {1, 2};
    int* it = std::search(hay, hay + 6, needle, needle + 2);
    std::cout << (it - hay) << ": " << *it << " " << *(it + 1) << "\n";
    int miss[2] = {9, 9};
    int* it2 = std::search(hay, hay + 6, miss, miss + 2);
    std::cout << (it2 == hay + 6 ? "end" : "found") << " " << (it2 - hay) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 r_return.cpp -o r_return_prog
$ ./r_return_prog
1: 1 2
end 6

Read in order: the needle 1, 2 occurs twice in the array 5, 1, 2, 3, 1, 2 - at index 1 and at index 4 - and the returned pointer sits at index 1, the first occurrence. The needle 9, 9 does not occur at all, and the returned pointer is the past-the-end pointer, 6 past the start. So a caller tests the return against the end of the source range to learn whether the search found anything; any other return value is a position in the range.

The empty cases: an empty needle, and an empty source range

Both empty cases follow the same return rule, and neither is an error:

C++
#include <algorithm>
#include <iostream>
int main() {
    int hay[5] = {1, 2, 3, 4, 5};
    int empty_needle[1] = {0};
    int* a = std::search(hay, hay + 5, empty_needle, empty_needle);
    std::cout << (a - hay) << "\n";
    int needle[2] = {3, 4};
    int* b = std::search(hay, hay, needle, needle + 2);
    std::cout << (b == hay ? "begin" : "other") << " " << (b - hay) << "\n";
    int* c = std::search(hay, hay, empty_needle, empty_needle);
    std::cout << (c == hay ? "begin" : "other") << " " << (c - hay) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 k_counts.cpp -o k_counts_prog
$ ./k_counts_prog
0
begin 0
begin 0

Read in order: an empty needle searched for in a non-empty range returns the start of the source range - offset 0 - because an empty target is found immediately, at the first position. A non-empty needle searched for in an empty source range returns that range's start, which for an empty range is also its end - the miss return, since the end of the source range is what a miss returns, and here begin and end are the same pointer. An empty needle in an empty range returns the same pointer again. So code that distinguishes found from not-found by comparing the return with the end of the source range reads an empty-needle search as found, at the start - which is the documented behavior, not a special case to guard against separately.

Versions: the classic call is old, and there is no version second cause

Unlike the C++17 names in this family, the classic std::search does not disappear under an older standard. The same call, with <algorithm> included, compiles under C++98:

C++
#include <algorithm>
int main() {
    int hay[5] = {1, 2, 3, 4, 5};
    int needle[2] = {3, 4};
    std::search(hay, hay + 5, needle, needle + 2);
    return 0;
}
Command
$ g++ -std=c++98 c_search98.cpp -o c_search98_prog
$ ./c_search98_prog

The program compiles and runs with no output, under the oldest standard flag probed here. So for the classic four-iterator call, a missing include is the only cause of this error: if <algorithm> is in the file, changing the standard flag is not the fix, because the name was already there in C++98. The newer pieces are additions around that classic core - the execution-policy overloads and the searcher overload, the form that takes a std::default_searcher, std::boyer_moore_searcher, or std::boyer_moore_horspool_searcher object, are C++17, and the classic overloads became constexpr in C++20 - so a version cause exists only for code that calls one of those newer forms, not for the four-iterator call this guide is about.

The neighboring name: search_n is not in either

The name std::search is used alongside does not live in the container header either. std::search_n - the search for a run of consecutive copies of one value - lives in <algorithm>, and it fails in a file whose only include is <vector> with its own plain error and no suggestion tail. It is shown here only as a neighbor, not re-taught:

C++
#include <vector>
int main() {
    int hay[6] = {1, 2, 2, 2, 3, 4};
    std::search_n(hay, hay + 6, 3, 2);
    return 0;
}
Terminal
$ g++ -std=c++17 b_searchn_vector.cpp -o b_searchn_vector_prog
b_searchn_vector.cpp: In function 'int main()':
b_searchn_vector.cpp:4:10: error: 'search_n' is not a member of 'std'
    4 |     std::search_n(hay, hay + 6, 3, 2);
      |          ^~~~~~~~

The neighboring name, fixed

The two names together take the same one added header - <algorithm> covers both. The fixed neighbor program reports 1: 2 for three consecutive copies of the value 2 in the array 1, 2, 2, 2, 3, 4 - the run starts at index 1, and the value there is 2:

C++
#include <algorithm>
#include <iostream>
int main() {
    int hay[6] = {1, 2, 2, 2, 3, 4};
    int* it = std::search_n(hay, hay + 6, 3, 2);
    std::cout << (it - hay) << ": " << *it << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 b_searchn_fixed.cpp -o b_searchn_fixed_prog
$ ./b_searchn_fixed_prog
1: 2

A different wording - and no argument-dependent rescue for this name

Including <algorithm> is not the only half of the name. This program includes the header, drops the std:: qualifier, and passes plain arrays:

C++
#include <algorithm>
int main() {
    int hay[5] = {1, 2, 3, 4, 5};
    int needle[2] = {3, 4};
    search(hay, hay + 5, needle, needle + 2);
    return 0;
}
Terminal
$ g++ -std=c++17 g_unqualified.cpp -o g_unqualified_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:5:5: error: 'search' was not declared in this scope; did you mean 'std::search'?
    5 |     search(hay, hay + 5, needle, needle + 2);
      |     ^~~~~~
      |     std::search
In file included from /usr/include/c++/13/algorithm:73,
                 from g_unqualified.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:117:1: note: 'std::search' declared here
  117 | search(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first,
      | ^~~~~~

That wording - was not declared in this scope, with did you mean 'std::search'? on the error line and a note pointing at a declaration - is a different defect with a different fix: the header is present, the qualifier is not. Qualify the name as std::search; adding includes does not bring the name out of namespace std. Arrays are fundamental types and have no associated namespace, so argument-dependent lookup has nothing to find the name from in this call.

The exact rule

  • std::search is declared by #include <algorithm>, and by no other header by rule. In the sweep probed here, every one of the other 101 headers in the library's header set - among them <vector>, <string>, <iostream>, <numeric>, and <version> - does not declare it, and neither does a file with no include at all.
  • 'search' is not a member of 'std' means the <algorithm> include is missing and nothing else in the file pulled the name in. In every context probed here, GCC prints no note lines and no help lines for this name; the only variation is the suggestion tail: 79 failing sole headers print the plain error, and 22 print the same error with ; did you mean 'bsearch'? appended.
  • The bsearch suggestion is not a fix in either shape: std::bsearch is a binary search for a single value in a sorted range, and std::search is a subsequence search in a general range. The tail appears when a header in the file has declared std::bsearch, and it names the nearest declared name, not the missing name's home.
  • In the sweep probed here, four headers besides <algorithm> rescue std::search transitively - <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 pull std::search in.
  • The return rules are the documented ones: the fixed programs here print 6: 7 8 9 for the subsequence 7, 8, 9 in the population 1 through 10, 1: 1 2 for the first of two occurrences of the needle 1, 2, and end 6 for a needle that does not appear - the past-the-end iterator is the miss return.
  • The empty cases follow the same rule: an empty needle returns the start of the source range (offset 0 in the program here), and an empty source range returns its own start, which is also its end, for an empty or a non-empty needle alike.
  • The classic four-iterator std::search is a C++98-era function: it compiles under C++98 with <algorithm> included, so the classic call has no too-old-standard second cause. The execution-policy and searcher overloads are C++17, and the classic overloads are constexpr since C++20.
  • Both iterator pairs must meet the forward-iterator requirements - a single-pass input range is not a legal source range for this function, unlike its neighbor std::sample.
  • std::search_n is not declared by #include <vector>: in the program here, which includes only <vector>, it gets its own is not a member of 'std' error, with no suggestion tail - and the fixed neighbor program, which includes <algorithm>, prints 1: 2 for the run search.
  • 'search' was not declared in this scope for an unqualified search use is a different defect: qualify the name as std::search - the include alone does not bring the name out of namespace std. For this name no argument-dependent-lookup case rescues the unqualified call in the program probed here: array arguments have no associated namespace.

The fixes, ranked

  • First, check the include list of the file that names std::search. If <algorithm> is not there, add it - that is the fix in the common case, and it holds whether the file failed in a lean context, failed in a textbook file, or happened to compile before through a transitive rescue.
  • Second, do not follow the bsearch suggestion if one appears. It names a different function with different requirements - a sorted range and a single key - and switching to it changes what the program computes. Take the fix from the name's home header, <algorithm>.
  • Third, do not be reassured by <iostream>, <vector>, or <string> being present - in the sweep here none of them rescues this name; <iostream> and <string> are on the suggestion side of the failing split and <vector> is on the plain side - and do not remove <algorithm> because a file compiles without it: in the probed set four headers rescue it, and those rescues are not guaranteed.
  • Fourth, do not chase the standard flag for the classic call. With <algorithm> included, the four-iterator call compiles as far back as C++98; a standard cause exists only when the call uses an execution policy or a searcher object, which are C++17 forms.
  • Fifth, if the error says was not declared in this scope instead, fix the name's qualifier to std::search as well - and do not look for an argument-dependent-lookup case that makes the unqualified call work over plain arrays, because there is none.
  • Sixth, if the failing name is the neighboring one, the fix is the same header: search_n takes an added <algorithm> too - one header for both search names.

Which fix for which cause

  • #include <algorithm> missing, std::search used in a file whose only include is one of the 79 headers listed as plain-failing in the sweep section above, or that includes nothing: the message is 'search' is not a member of 'std', with no suggestion tail, no note lines, and no help lines - add #include <algorithm>.
  • #include <algorithm> missing, std::search used in a file whose only include is one of the 22 headers listed as suggestion-failing in the sweep section above: the message is 'search' is not a member of 'std'; did you mean 'bsearch'?, with no note lines and no help lines - add #include <algorithm>, and do not switch the call to std::bsearch.
  • std::search used in a textbook file including <iostream> and <vector>: the message carries the bsearch suggestion - add #include <algorithm>.
  • std::search used with only <algorithm>, <execution>, <functional>, <regex>, or <valarray> included: no error on this library version - add #include <algorithm> anyway when it is not the include already present, because the other four rescues are not guaranteed.
  • std::search called with an execution policy or a searcher object under a standard before C++17: those overloads do not exist yet in that standard - compile as C++17 or later, or use the classic four-iterator call, which needs no newer standard than C++98.
  • std::search_n used without <algorithm> in a file whose only include is <vector>: the message names search_n, with no suggestion tail - add #include <algorithm>. For that name the header is the same one.
  • search used without the std:: qualifier, <algorithm> included, over array arguments: the message is 'search' was not declared in this scope; did you mean 'std::search'? - qualify the name as std::search.

Checklist

  • Check the include list of the file that names the call, not just the project. The include must be in that file, or in a header that file includes directly by design.
  • Read the suggestion tail before acting on it. For this name the tail, when it appears, names bsearch, a different function; its presence or absence changes nothing about the fix.
  • Do not assume the container headers rescue this name, and do not delete a working <algorithm> line because a file compiles without it. In the sweep here <vector> fails plain, <string> and <iostream> fail with the suggestion, and the four headers that do rescue the name do so only transitively.
  • Do not size the result from the needle or test it loosely: a found search returns the position of the first occurrence, and a miss returns the end of the source range. Compare the return with the source range's end to tell the two apart.
  • Do not treat an empty needle as a miss: it returns the start of the source range. An empty source range returns its start, which is its end, for any needle.
  • Match the form to the standard only where a newer form is used: the classic call needs <algorithm> and no standard newer than C++98; the policy and searcher forms need C++17.
  • After the fix, confirm the program runs, not just that it compiles: the fixed programs here print 6: 7 8 9 for the vector search, 1: 1 2 and end 6 for the return-value calls, and 1: 2 for the neighbor search_n call.
  • 'std::sample' Is Not a Member of 'std' Fix - the same missing-include shape for the other subsequence-style <algorithm> name in this family, with the same five-entry rescue set: its sweep splits into 101 uniform bare failures and four transitive rescues, where this guide's sweep splits into 79 plain failures, 22 failures with the bsearch suggestion tail, and the same four rescues - and the two guides differ in the iterator floor as well, since std::sample accepts an input-iterator population where std::search requires forward iterators for both ranges.
  • 'std::find' Is Not a Member of 'std' Fix - the same missing-include shape for the single-element search in the same header: std::find looks for one value in a range where this guide's call looks for a whole subsequence, and both names take the same one added header, <algorithm>.

Sources

  • cppreference, std::search — for std::search being a function template defined in <algorithm>; for the four-iterator overloads searching for the first occurrence of the target range in the source range, with elements compared by operator== or by a given binary predicate; for both iterator pairs needing the forward-iterator requirements; for the return value being an iterator to the beginning of the first occurrence, the start of the source range when the target range is empty, and the end of the source range when the target range does not appear; for the complexity being at most N1 times N2 comparisons; for the execution-policy overloads and the searcher overload being C++17 additions, with the standard searchers std::default_searcher, std::boyer_moore_searcher, and std::boyer_moore_horspool_searcher; for the classic overloads being constexpr since C++20; and for the defect reports applied to C++98 that settle the empty-target and not-found return values, which place the classic overloads in C++98.
  • cppreference, Standard library header <algorithm> — for <algorithm> being the header whose synopsis declares std::search and std::search_n, the home-header fact both the fix and the neighbor sections of this guide rest on.

Keep Exploring