C++
'std::replace' Is Not a Member of 'std' Fix
std::replace lives in one header, <algorithm> - and in the sweep probed here, covering all 106 standard headers in this library's header set, only 4 other headers pull it in on the side, so the textbook <vector> program fails outright. This name's diagnostic is the plainest in its family: 100 of the 101 failing sole-header contexts give the one bare error line with no suggestion, no notes, and no help, and the single exception - <filesystem> - offers a suggestion that names an enumerator, not a function. This guide pins down the failing shapes, the one namesake suggestion, why the call returns nothing and moves nothing, and the include that fixes it by rule.
Written December 3, 2026 · Tested with GCC 13.3.0 · 26 min read
- The error: the include is missing
- The textbook version fails too - with the same plain error
- The one suggestion in the sweep: an enumerator, not a function
- Neighboring headers: 100 fail plainly, 1 offers an enumerator, and 4 rescue it by accident
Written December 3, 2026
Tested with GCC 13.3.0
What this guide is
std::replace is a function template defined in the header <algorithm>. It walks a range once and overwrites every element that is equal to a given old value - comparing with operator== - with a given new value, in place. It returns nothing, moves nothing, and changes no size: the elements that do not match keep their values and their positions, and the replaced elements sit where they always sat, holding the new value. A sibling, std::replace_if, replaces the elements a predicate accepts instead. The classic call predates C++11, 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 - 'replace' 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 4 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 - and this name's one trap is a single suggestion, in a single header, that names something which is not a function at all, covered below.
And for this name the missing include is the only cause of the classic call's error. Some names in this family hide a second cause behind the same wording - a standard floor the name starts at - but std::replace predates C++11, so a file that includes <algorithm> compiles this call at every standard level probed here, C++98 included, as the versions section below shows. The pieces layered on top do have floors - the execution-policy overload needs C++17, and constant-expression use needs C++20 - but those floors belong to the added pieces, not to the classic call.
This name's diagnostic is the plainest in the family so far. Of the 101 failing sole-header contexts probed here, 100 fail with the plain line alone - no suggestion tail, no note lines, no help lines - and the one exception, <filesystem>, fails with a suggestion that names an enumerator of a filesystem permissions enum, not a callable name. No failing context in the sweep produced a different error at all. 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 5 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 replaces the 1s in a five-element array, 1, 2, 1, 3, 1, with 9, but includes only <limits>:
#include <limits>
int main() {
int a[5] = {1, 2, 1, 3, 1};
std::replace(a, a + 5, 1, 9);
return 0;
}
-std=c++17 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:4:10: error: 'replace' is not a member of 'std'
4 | std::replace(a, a + 5, 1, 9);
| ^~~~~~~
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++17 n_none.cpp -o n_none_prog
n_none.cpp: In function 'int main()':
n_none.cpp:3:10: error: 'replace' is not a member of 'std'
3 | std::replace(a, a + 5, 1, 9);
| ^~~~~~~
The textbook version fails too - with the same plain error
The version most often written first - replace the 1s in a vector with 9 and print the vector - produces an error in this library, without <algorithm>, and the error is the plain one from the previous section:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 1, 3, 1};
std::replace(v.begin(), v.end(), 1, 9);
std::cout << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v.size() << "\n";
return 0;
}
-std=c++17 t_textbook.cpp -o t_textbook_prog
t_textbook.cpp: In function 'int main()':
t_textbook.cpp:5:10: error: 'replace' is not a member of 'std'
5 | std::replace(v.begin(), v.end(), 1, 9);
| ^~~~~~~
Nothing rescues the call here and nothing distorts it either: <vector> does not pull the algorithm in - in the sweep below it fails for this name, plainly - and <iostream> pulls in no namesake for this name, so the compiler reports the missing name itself. A program that uses std::replace 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++17 i_algorithm.cpp -o i_algorithm_prog
$ ./i_algorithm_prog
The one suggestion in the sweep: an enumerator, not a function
Exactly one header in the sweep decorates this error, and what it offers is not callable. In a file whose only include is <filesystem>, the compiler can see a name spelled replace - an enumerator of std::filesystem::perm_options, the scoped enum that says how a permissions call should treat existing permissions - and it offers that name as the guess:
-std=c++17 s_filesystem.cpp -o s_filesystem_prog
s_filesystem.cpp: In function 'int main()':
s_filesystem.cpp:4:10: error: 'replace' is not a member of 'std'; did you mean 'std::filesystem::perm_options::replace'?
4 | std::replace(a, a + 5, 1, 9);
| ^~~~~~~
In file included from /usr/include/c++/13/filesystem:48,
from s_filesystem.cpp:1:
/usr/include/c++/13/bits/fs_fwd.h:210:7: note: 'std::filesystem::perm_options::replace' declared here
210 | replace = 0x1,
| ^~~~~~~
Read the note at the bottom: the suggested name is declared as replace = 0x1, a constant of an enum, the flag a program passes when it wants a permissions update to replace the current permissions rather than add to them. It takes no arguments and replaces no elements. This failing context is also the only one in the sweep that carries note lines at all. Following the suggestion would rename the call to a value, not to a function - the fix for this file is the same one include as everywhere else.
Neighboring headers: 100 fail plainly, 1 offers an enumerator, and 4 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 replaces the 1s in 1, 2, 1, 3, 1 with 9 and checks that the range reads 9, 2, 9, 3, 9 afterward, the outcome splits by compile result three ways - the failure in two diagnostic shapes, 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><chrono><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><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. <ranges> fails plainly for the classic name too: the C++20 ranges library has its own replace 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 works on - fails plainly, as the textbook section above shows.
The second shape is the failure with a suggestion, and it has exactly one member: <filesystem>, shown in the previous section, offering the perm_options::replace enumerator. No other header in the sweep offers any name at all - there is no crowd of near-miss functions for this name, because in this library almost nothing declares a name spelled near replace in namespace std itself. No failing header in any shape produced a help line, and no failing header produced a different error: this name never gets handed to a namesake function, because the standard library has no second function by this name for the call to be measured against.
And these headers rescue the program - it compiles and runs with no <algorithm> anywhere in the file:
<algorithm><execution><functional><regex><valarray>
The declaring header in this library sits underneath very little - a policy header, a function-object header, the regular-expression header, and the numeric-array header - so the rescue set runs to only 4 transitive entries. That is also why the failing set is nearly the whole library here: with 101 of the other 105 headers failing in one shape or another, 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 4 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 = {1, 2, 1, 3, 1};
std::replace(v.begin(), v.end(), 1, 9);
std::cout << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v.size() << "\n";
return 0;
}
-std=c++17 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
9 2 9 3 9 5
One include, <algorithm>, declares the function - and the textbook program prints 9 2 9 3 9 5: every 1 overwritten with 9 where it stood, the elements that were not 1 untouched, and the size still 5. The name is now declared by a header the file names itself, instead of by an internal include of one of the 4 rescuing headers that a different library version is free to rearrange. For a file that does fail - the <limits> shape above, or the <filesystem> shape - the same one include is the whole fix.
What the call does: it overwrites in place and returns nothing
The call returns no value at all, and that is this name's one real subtlety: there is no new end to capture, no count of what changed, and no tail of unspecified values - every position in the range still holds a meaningful value when the call finishes, the matched ones holding the new value and the rest holding what they always held. Asking the call for a result is itself a compile error:
-std=c++17 r_void.cpp -o r_void_prog
r_void.cpp: In function 'int main()':
r_void.cpp:5:10: error: deduced type 'void' for 'e' is incomplete
5 | auto e = std::replace(v.begin(), v.end(), 1, 9);
| ^
Beyond that, the behavior is the direct one: every element equal to the old value is replaced, all of them, wherever they stand; a value that is absent changes nothing; replacing in a range where every element matches overwrites every element; an empty range is a no-op; and the elements that are not replaced keep their values and their order, because nothing is ever moved:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 1, 3, 1};
std::replace(v.begin(), v.end(), 1, 9);
std::cout << "all";
for (int n : v) std::cout << " " << n;
std::cout << "\n";
std::cout << "size " << v.size() << "\n";
std::vector<int> w = {3, 1, 4, 1, 5};
std::replace(w.begin(), w.end(), 9, 0);
std::cout << "absent";
for (int n : w) std::cout << " " << n;
std::cout << "\n";
std::vector<int> x = {7, 7, 7};
std::replace(x.begin(), x.end(), 7, 8);
std::cout << "every";
for (int n : x) std::cout << " " << n;
std::cout << "\n";
std::vector<int> mt;
std::replace(mt.begin(), mt.end(), 1, 9);
std::cout << "empty " << mt.size() << "\n";
std::vector<int> s = {5, 1, 5, 2, 5, 3};
std::replace(s.begin(), s.end(), 5, 0);
std::cout << "order";
for (int n : s) std::cout << " " << n;
std::cout << "\n";
return 0;
}
-std=c++17 r_semantics.cpp -o r_semantics_prog
$ ./r_semantics_prog
all 9 2 9 3 9
size 5
absent 3 1 4 1 5
every 8 8 8
empty 0
order 0 1 0 2 0 3
Read in order: the all line reports 9 2 9 3 9 - all three 1s overwritten where they stood, the 2 and the 3 untouched between them. The size line reports 5: nothing was added or removed, so the vector is exactly as long as before the call, and unlike the compaction algorithms there is no tail of unspecified values to avoid - every position still holds a value the program put there or a value the call put there. The absent line reports 3 1 4 1 5, the input unchanged: replacing a value that is not there rewrites nothing. The every line reports 8 8 8: when every element matches, every element is overwritten. The empty line reports 0: an empty range stays empty and the call is a no-op. The order line reports 0 1 0 2 0 3: the 5s of 5, 1, 5, 2, 5, 3 became 0s in place, and the surviving 1, 2, 3 never moved - this call overwrites, it does not rearrange.
Set against its closest neighbor, the trade is exact. std::remove on the same input reports a kept count and leaves the size alone too - but what it keeps is a shorter logical range in front of an unspecified tail, and what it returns is the boundary. This call keeps the whole range meaningful and returns nothing:
-std=c++17 r_remove_contrast.cpp -o r_remove_contrast_prog
$ ./r_remove_contrast_prog
replace size 5
remove size 5 kept 2
Both lines report size 5. The difference is in what the 5 positions mean afterward: after replace, all five are the answer; after remove, only the first 2 are, and the caller still owes the container an erase call to make the size honest.
Forward iterators: the weakest iterators it accepts
The call needs forward iterators - and writable ones, because it overwrites elements by assignment. That is one category stronger than a pure reading algorithm needs, and it is why the call cannot run over a stream. A list's iterators qualify, and the call overwrites a list the same way it overwrites a vector:
#include <algorithm>
#include <iostream>
#include <list>
int main() {
std::list<int> l = {1, 2, 1, 3, 1};
std::replace(l.begin(), l.end(), 1, 9);
std::cout << "list";
for (int n : l) std::cout << " " << n;
std::cout << " " << l.size() << "\n";
return 0;
}
-std=c++17 r_list.cpp -o r_list_prog
$ ./r_list_prog
list 9 2 9 3 9 5
It reports list 9 2 9 3 9 5 - every 1 in the list overwritten with 9, and the size still 5, with no erase step owed to anyone: the call never pretended to change the length. The policy overloads ask for forward iterators as well, for the same reason: a parallel execution may need to revisit the range.
Versions: no second cause for the classic call - it predates C++11
This call needs no modern-enough standard, and that is what separates it from some of its neighbors. std::replace predates C++11 - the cppreference page carries no "since" marker on the classic overload. The fixed programs in this guide are compiled as C++17, but there is no floor below that to hit for the classic call: with <algorithm> included, the same replacement compiled as C++98 compiles and runs:
#include <algorithm>
#include <iostream>
int main() {
int a[5] = {1, 2, 1, 3, 1};
std::replace(a, a + 5, 1, 9);
std::cout << a[0] << " " << a[1] << " " << a[2] << " " << a[3] << " " << a[4] << "\n";
return 0;
}
-std=c++98 c_version98.cpp -o c_version98_prog
$ ./c_version98_prog
9 2 9 3 9
And it compiles and runs as C++11 the same way, printing 9 2 9 3 9. So this name's error has one cause, not two: add <algorithm>, and no standard flag needs to change with it. A file that includes <algorithm> and still sees this error does not have a version problem with the classic call; the include is not reaching the file that names the call, or the name in the file is not this name.
The pieces layered on top of the classic call do carry floors, and naming one splits in two when a reader tries it. 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 = {1, 2, 1, 3, 1};
std::replace(std::execution::seq, v.begin(), v.end(), 1, 9);
return (v[0] == 9 && v[2] == 9 && v[4] == 9) ? 0 : 1;
}
-std=c++17 p_policy_algonly.cpp -o p_policy_algonly_prog
p_policy_algonly.cpp: In function 'int main()':
p_policy_algonly.cpp:5:23: error: 'std::execution' has not been declared
5 | std::replace(std::execution::seq, v.begin(), v.end(), 1, 9);
| ^~~~~~~~~
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::replace exists:
-std=c++17 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:23: error: 'std::execution' has not been declared
6 | std::replace(std::execution::seq, v.begin(), v.end(), 1, 9);
| ^~~~~~~~~
And since C++20 the classic call is also usable in constant expressions - this program's replacement 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, 5> a{1, 2, 1, 3, 1};
std::replace(a.begin(), a.end(), 1, 9);
return (a[0] == 9 && a[1] == 2 && a[2] == 9 && a[3] == 3 && a[4] == 9) ? 1 : 0;
}
static_assert(check());
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:17: warning: call to non-'constexpr' function 'void std::replace(_FIter, _FIter, const _Tp&, const _Tp&) [with _FIter = int*; _Tp = int]' [-Winvalid-constexpr]
5 | std::replace(a.begin(), a.end(), 1, 9);
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/13/algorithm:61,
from c_constexpr17.cpp:1:
/usr/include/c++/13/bits/stl_algo.h:4369:5: note: 'void std::replace(_FIter, _FIter, const _Tp&, const _Tp&) [with _FIter = int*; _Tp = int]' declared here
4369 | replace(_ForwardIterator __first, _ForwardIterator __last,
| ^~~~~~~
c_constexpr17.cpp: At global scope:
c_constexpr17.cpp:8:20: error: non-constant condition for static assertion
8 | static_assert(check());
| ~~~~~^~
c_constexpr17.cpp:8:20: error: 'constexpr int check()' called in a constant expression
c_constexpr17.cpp:3:15: note: 'constexpr int check()' declared here
3 | constexpr int check() {
| ^~~~~
So C++20 is the floor for that layered piece and C++17 is the floor for the policy form - and no standard at any level rescues a missing include, while no include is needed beyond <algorithm> for the classic call at any standard level.
The neighboring names: one side door in the set, and it is not this family
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> the replace family all fails - there is no side door for any of them in this library, because the header that declares this family is one <vector> does not pull in. std::replace_if - which replaces the elements a predicate accepts - and std::replace_copy - which writes the replaced range into a second range and leaves the source alone - fail with the plain error:
-std=c++17 b_replaceif_vector.cpp -o b_replaceif_vector_prog
b_replaceif_vector.cpp: In function 'int main()':
b_replaceif_vector.cpp:4:10: error: 'replace_if' is not a member of 'std'
4 | std::replace_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; }, 0);
| ^~~~~~~~~~
-std=c++17 b_replacecopy_vector.cpp -o b_replacecopy_vector_prog
b_replacecopy_vector.cpp: In function 'int main()':
b_replacecopy_vector.cpp:5:19: error: 'replace_copy' is not a member of 'std'
5 | auto e = std::replace_copy(v.begin(), v.end(), w.begin(), 1, 9);
| ^~~~~~~~~~~~
std::unique - which compacts away consecutive duplicates - fails plainly in the same file, while its other neighbors fail with suggestions: std::remove is offered move, and std::remove_if is offered remove_cv:
-std=c++17 b_unique_vector.cpp -o b_unique_vector_prog
b_unique_vector.cpp: In function 'int main()':
b_unique_vector.cpp:4:19: error: 'unique' is not a member of 'std'
4 | auto e = std::unique(v.begin(), v.end());
| ^~~~~~
-std=c++17 b_remove_vector.cpp -o b_remove_vector_prog
b_remove_vector.cpp: In function 'int main()':
b_remove_vector.cpp:4:19: error: 'remove' is not a member of 'std'; did you mean 'move'?
4 | auto e = std::remove(v.begin(), v.end(), 1);
| ^~~~~~
| move
-std=c++17 b_removeif_vector.cpp -o b_removeif_vector_prog
b_removeif_vector.cpp: In function 'int main()':
b_removeif_vector.cpp:4:19: error: 'remove_if' is not a member of 'std'; did you mean 'remove_cv'?
4 | auto e = std::remove_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; });
| ^~~~~~~~~
| remove_cv
And one name in the probed set does not fail at all: std::copy, called in a file whose only include is <vector>, compiles and runs - it is declared by a lower-level header this library's <vector> does pull in, so it has the side door the replace family lacks. That contrast is the lesson of the set: whether a missing <algorithm> shows itself depends on which internal header declares the name, and the only way to know for a given name is to probe it - the replace family, probed here, is uniformly outside. The failing neighbors are shown here only as neighbors, not re-taught.
The neighboring names, fixed
Together the five names take one header between them - <algorithm> for all of them, with <vector> covering only the containers they work on. The fixed neighbor program reports all five operations:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 1, 3, 1};
std::replace(v.begin(), v.end(), 1, 9);
std::cout << "replace";
for (int n : v) std::cout << " " << n;
std::cout << "\n";
std::vector<int> w = {1, 2, 3, 4, 5, 6};
std::replace_if(w.begin(), w.end(), [](int x) { return x % 2 == 0; }, 0);
std::cout << "replace_if";
for (int n : w) std::cout << " " << n;
std::cout << "\n";
std::vector<int> x = {1, 2, 1, 3, 1};
auto e = std::remove(x.begin(), x.end(), 1);
std::cout << "remove";
for (auto it = x.begin(); it != e; ++it) std::cout << " " << *it;
std::cout << "\n";
std::vector<int> y = {1, 1, 2, 2, 3};
auto e2 = std::unique(y.begin(), y.end());
std::cout << "unique";
for (auto it = y.begin(); it != e2; ++it) std::cout << " " << *it;
std::cout << "\n";
std::vector<int> z = {1, 2, 1, 3};
std::vector<int> out(4);
std::replace_copy(z.begin(), z.end(), out.begin(), 1, 9);
std::cout << "replace_copy";
for (int n : out) std::cout << " " << n;
std::cout << "\n";
return 0;
}
-std=c++17 b_neighbors_fixed.cpp -o b_neighbors_fixed_prog
$ ./b_neighbors_fixed_prog
replace 9 2 9 3 9
replace_if 1 0 3 0 5 0
remove 2 3
unique 1 2 3
replace_copy 9 2 9 3
Read in order: replace has overwritten every 1 in its range with 9, in place; replace_if, under a predicate that accepts the even values, has overwritten them with 0, reporting 1 0 3 0 5 0; remove leaves 2, 3 in front of the new logical end; unique leaves 1, 2, 3 in front of its new logical end, the consecutive duplicates compacted away; and replace_copy has written 9 2 9 3 into its second range, the replaced form of its source, which it leaves untouched.
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::replace is a function object in the ranges namespace, declared in <algorithm>, that accepts a whole range and returns the range's past-the-end iterator - unlike its classic counterpart, which returns nothing, the ranges form hands back the end it walked to. Compiled as C++20 it overwrites the same vector and reports the same shape of answer:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 1, 3, 1};
auto r = std::ranges::replace(v, 1, 9);
std::cout << "ranges";
for (int n : v) std::cout << " " << n;
std::cout << " " << (r == v.end()) << "\n";
return 0;
}
-std=c++20 r_ranges.cpp -o r_ranges_prog
$ ./r_ranges_prog
ranges 9 2 9 3 9 1
The trailing 1 is the comparison against the vector's end: the returned iterator is the past-the-end iterator, exactly as documented. Two boundaries and one library behavior 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::replace(v, 1, 9);
| ^~~~~~
And in this library the ranges header does not rescue the ranges form - a file whose only include is <ranges> fails for it with the plain error, and a file whose only include is <array> fails for it the same way, the two transcripts identical in shape:
-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: 'replace' is not a member of 'std::ranges'
5 | auto r = std::ranges::replace(a, 1, 9);
| ^~~~~~~
-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: 'replace' is not a member of 'std::ranges'
4 | auto r = std::ranges::replace(a, 1, 9);
| ^~~~~~~
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::replace with the plain error, 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[5] = {1, 2, 1, 3, 1};
replace(a, a + 5, 1, 9);
return 0;
}
-std=c++17 g_unqualified.cpp -o g_unqualified_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:4:5: error: 'replace' was not declared in this scope; did you mean 'std::replace'?
4 | replace(a, a + 5, 1, 9);
| ^~~~~~~
| std::replace
In file included from /usr/include/c++/13/algorithm:73,
from g_unqualified.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:174:1: note: 'std::replace' declared here
174 | replace(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value,
| ^~~~~~~
That wording - was not declared in this scope, with did you mean 'std::replace'? 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::replace; adding includes does not bring the name out of namespace std. Swap the arrays for a vector, and the same unqualified call compiles and runs:
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 1, 3, 1};
replace(v.begin(), v.end(), 1, 9);
return (v[0] == 9 && v[2] == 9 && v[4] == 9) ? 0 : 1;
}
-std=c++17 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 vector's iterators are std types, so their associated namespace is std, and lookup finds std::replace through them. A list's iterators rescue it the same way - the same unqualified call over a list compiles and runs:
-std=c++17 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, 5> a{1, 2, 1, 3, 1};
replace(a.begin(), a.end(), 1, 9);
return (a[0] == 9) ? 0 : 1;
}
-std=c++17 g_unqualified_stdarray.cpp -o g_unqualified_stdarray_prog
g_unqualified_stdarray.cpp: In function 'int main()':
g_unqualified_stdarray.cpp:5:5: error: 'replace' was not declared in this scope; did you mean 'std::replace'?
5 | replace(a.begin(), a.end(), 1, 9);
| ^~~~~~~
| std::replace
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:174:1: note: 'std::replace' declared here
174 | replace(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value,
| ^~~~~~~
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. Write the qualifier either way.
The exact rule
std::replaceis declared by#include <algorithm>, and by no other header by rule. In the sweep probed here, 101 of the other 105 headers in the library's header set - among them<limits>,<ranges>,<vector>, and<string>- do not declare it, and neither does a file with no include at all; the remaining 4 pull it in transitively, as this library version's behavior, not as a guarantee.'replace' 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 - and it comes in two shapes in the sweep probed here: 100 failing sole headers fail with one error line and no suggestion tail, no note lines, and no help lines; and 1,<filesystem>, fails with a suggestion namingstd::filesystem::perm_options::replace, an enumerator of a permissions enum, with a note pointing at that enumerator's declaration. No failing sole header produced a different error.- In the sweep probed here, 4 headers besides
<algorithm>rescuestd::replacetransitively -<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::replacein. - The result rules are the documented ones, as the fixed programs here show: the fixed program prints
9 2 9 3 9 5for the textbook case; the semantics program printsall 9 2 9 3 9,size 5,absent 3 1 4 1 5,every 8 8 8,empty 0, andorder 0 1 0 2 0 3- the call overwrites every element equal to the old value with the new value, in place, returns nothing, and leaves the range's size and every unmatched element untouched, with no unspecified tail. - The contrast with the compaction neighbor follows from the same rule: the contrast program prints
replace size 5andremove size 5 kept 2, becausestd::removeleaves only the kept prefix meaningful and returns its end, while this call leaves all 5 positions meaningful and returns nothing - capturing its result in anautovariable is a compile error,deduced type 'void' for 'e' is incomplete. - The classic call's iterators must be forward iterators, and writable: the list program here overwrites a list and prints
list 9 2 9 3 9 5. - The classic
std::replacepredates C++11: with<algorithm>included it compiles and runs as C++98, reporting9 2 9 3 9, and as C++11 the same way. The layered pieces have floors: the execution-policy overload starts at C++17 - with only<algorithm>included, namingstd::execution::seqfails with'std::execution' has not been declared; with<execution>included alongside it, the policy call compiles and runs; and compiled as C++14, the same program fails on the policy's name. 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::replace_if,std::replace_copy, andstd::uniqueare declared by#include <algorithm>as well and all fail in a sole<vector>file with the plain error;std::removefails there with the suggestiondid you mean 'move'?andstd::remove_ifwithdid you mean 'remove_cv'?; andstd::copycompiles in that file outright, the one side door in the probed set. The fixed neighbor program printsreplace 9 2 9 3 9,replace_if 1 0 3 0 5 0,remove 2 3,unique 1 2 3, andreplace_copy 9 2 9 3. std::ranges::replaceis a ranges form of this same name: a function object in namespacestd::ranges, declared in<algorithm>by the standard, needing C++20, returning the range's past-the-end iterator - compiled as C++17 the ranges program here fails with'std::ranges' has not been declared, and compiled as C++20 it printsranges 9 2 9 3 9 1for the five-element vector. In the probes here<ranges>alone does not rescue it in this library - it fails with the plain not-a-member error for the ranges form,<array>alone fails for it the same way, and<ranges>fails plainly for the classic name in the sweep.'replace' was not declared in this scopefor an unqualifiedreplaceuse is a different defect: qualify the name asstd::replace- 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. Qualify the name either way.
The fixes, ranked
- First, check the include list of the file that names
std::replace. If<algorithm>is not there, add it - that is the fix for the only cause of this error. It holds whether the file failed plainly - 100 of the 101 probed failures did - or with the one suggestion, and whether or not it happened to compile before through one of the 4 transitive rescues and stopped compiling when its other includes changed. - Second, do not follow the one suggestion this error offers here: in the probed set the compiler proposes
std::filesystem::perm_options::replacein exactly one context, and that name is an enumerator - a permissions flag with the value 0x1 - not a function. Renaming the call to it does not replace elements in a range. The fix for that file is the same include as everywhere else. - Third, do not chase a standard flag for the classic call: this name predates C++11, so no flag change fixes this error, and a file with
<algorithm>included that still fails has an include that is not reaching the translation unit, not a version problem. - Fourth, do not reach for a container's header because the call works on containers, 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 4 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. - Sixth, do not expect the call to hand anything back: it returns nothing, so there is no new end to capture and no count of replacements - if the count matters, count the matches before the call or compare afterward; and do not expect it to move or shorten anything, because every element stays in its position and the size stays put.
- 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
std::ranges::replaceneeds 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::replaceas 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::unique' Is Not a Member of 'std' Fix
- 'std::search_n' Is Not a Member of 'std' Fix
- 'std::remove' Is Not a Member of 'std' Fix
- 'std::remove_if' Is Not a Member of 'std' Fix
Sources
- cppreference, std::replace, std::replace_if - the function templates are defined in
<algorithm>; replace overwrites every element equal to the old value with the new value and returns void, replace_if overwrites the elements a predicate accepts; the execution-policy overloads are marked since C++17 and the classic overloads constexpr since C++20, with no since marker on the classic calls themselves. - cppreference,
<algorithm>header - the header that declares this name and the other algorithm names used as neighbors here. - cppreference, std::ranges::replace, std::ranges::replace_if - the C++20 ranges forms of this name, function objects in namespace
std::rangesdeclared in<algorithm>, returning the target range's past-the-end iterator.
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.