C++

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

std::remove 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. Worse, this name fails in three different wordings: 41 headers give the plain not-a-member line, 38 offer a suggestion that always names a wrong name ('move' in 28 of them), and 22 hand the call to a different std::remove entirely - the <cstdio> function that deletes files - and report a conversion error instead. This guide pins down the failing shapes, the one namesake trap, why the call never shrinks a container, and the include that fixes it by rule.

Written December 3, 2026 · Tested with GCC 13.3.0 · 28 min read

By the end of this guide
  • The error: the include is missing
  • The textbook version fails too - with a different error
  • The other std::remove: the one that deletes files
  • The fix

Written December 3, 2026

Tested with GCC 13.3.0

What this guide is

std::remove is a function template defined in the header <algorithm>. It shifts every element of a range that is not equal to a given value toward the front - comparing with operator== - keeps their relative order, and returns an iterator past the new logical end. It does not erase anything: the range keeps its physical size, and the elements between the returned iterator and the old end are still dereferenceable but hold unspecified values. A sibling, std::remove_if, removes 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 - 'remove' 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 adds two traps its neighbors do not have, a namesake function and a crowd of wrong suggestions, both 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::remove 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 least plain in the family so far. Of the 101 failing sole-header contexts probed here, only 41 fail with the plain line alone; 38 fail with a suggestion, and every suggestion names a wrong name - move in 28 files, remove_cv in 6, wmemmove in 3, and memmove in 1; and 22 fail with a conversion error instead of the not-a-member error at all, because a second std::remove - the file-deleting function from <cstdio> - is visible in those files and the call is measured against it. 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 removes the 1s from a five-element array, 1, 2, 1, 3, 1, but includes only <limits>:

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

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:

Output
-std=c++17 n_none.cpp -o n_none_prog
n_none.cpp: In function 'int main()':
n_none.cpp:3:10: error: 'remove' is not a member of 'std'
    3 |     std::remove(a, a + 5, 1);
      |          ^~~~~~

The textbook version fails too - with a different error

The version most often written first - remove the 1s from a vector and print what is left - produces an error in this library, without <algorithm>, and the error is not the one in the previous section:

C++
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {1, 2, 1, 3, 1};
    auto e = std::remove(v.begin(), v.end(), 1);
    std::cout << (e - v.begin()) << " " << v[0] << " " << v[1] << " " << v.size() << "\n";
    return 0;
}
Output
-std=c++17 t_textbook.cpp -o t_textbook_prog
t_textbook.cpp: In function 'int main()':
t_textbook.cpp:5:33: error: cannot convert 'std::vector<int>::iterator' to 'const char*'
    5 |     auto e = std::remove(v.begin(), v.end(), 1);
      |                          ~~~~~~~^~
      |                                 |
      |                                 std::vector<int>::iterator
In file included from /usr/include/c++/13/cstdio:42,
                 from /usr/include/c++/13/ext/string_conversions.h:45,
                 from /usr/include/c++/13/bits/basic_string.h:4109,
                 from /usr/include/c++/13/string:54,
                 from /usr/include/c++/13/bits/locale_classes.h:40,
                 from /usr/include/c++/13/bits/ios_base.h:41,
                 from /usr/include/c++/13/ios:44,
                 from /usr/include/c++/13/ostream:40,
                 from /usr/include/c++/13/iostream:41,
                 from t_textbook.cpp:1:
/usr/include/stdio.h:158:32: note:   initializing argument 1 of 'int remove(const char*)'
  158 | extern int remove (const char *__filename) __THROW;
      |                    ~~~~~~~~~~~~^~~~~~~~~~

Read the note at the bottom: the only remove this file can see is the one whose argument is a filename. <vector> does not pull the algorithm in - in the sweep below it fails for this name, with a suggestion - and <iostream> pulls in, through <string>, the C header that declares the file-removing namesake, so the compiler matches the call against that function and complains about the argument type instead of the missing name. A reader who searches for the conversion error lands nowhere near the real cause. A program that uses std::remove 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:

Terminal
-std=c++17 i_algorithm.cpp -o i_algorithm_prog
$ ./i_algorithm_prog

The other std::remove: the one that deletes files

This name is shared. The standard library also defines a std::remove that takes one const char* and deletes the file it names - the C function, declared in <cstdio> - the algorithm's own cppreference page notes this second overload explicitly. In a file whose only include is <cstdio>, the iterator call fails against that function, with a note pointing at its declaration:

Output
-std=c++17 a_cstdio.cpp -o a_cstdio_prog
a_cstdio.cpp: In function 'int main()':
a_cstdio.cpp:4:17: error: cannot convert 'int*' to 'const char*'
    4 |     std::remove(a, a + 5, 1);
      |                 ^
      |                 |
      |                 int*
In file included from /usr/include/c++/13/cstdio:42,
                 from a_cstdio.cpp:1:
/usr/include/stdio.h:158:32: note:   initializing argument 1 of 'int remove(const char*)'
  158 | extern int remove (const char *__filename) __THROW;
      |                    ~~~~~~~~~~~~^~~~~~~~~~

And that function is really there: a file that calls it with a filename compiles and runs in the same sole-<cstdio> context:

C++
#include <cstdio>
int main() {
    return std::remove("no_such_file_slot07.txt") != 0 ? 0 : 1;
}
Terminal
-std=c++17 a_cstdio_file.cpp -o a_cstdio_file_prog
$ ./a_cstdio_file_prog

So the conversion-error shape is not a broken diagnostic - it is the correct report for the wrong function. When the error for a std::remove call talks about const char*, the file is seeing the file-deleting namesake, and the fix is still the same one include: <algorithm> declares the algorithm overloads alongside it, and overload resolution then picks the iterator form.

Neighboring headers: 41 fail plainly, 38 offer a wrong name, 22 convert against the namesake, 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 removes the 1s from 1, 2, 1, 3, 1 and checks that the new logical end sits at index 2 with 2, 3 in front of it, the outcome splits by compile result four ways - the failure in three 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 41 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:

  • <barrier>
  • <cassert>
  • <cctype>
  • <cerrno>
  • <cfenv>
  • <cfloat>
  • <cinttypes>
  • <ciso646>
  • <climits>
  • <clocale>
  • <compare>
  • <concepts>
  • <csetjmp>
  • <csignal>
  • <cstdalign>
  • <cstdarg>
  • <cstdbool>
  • <cstddef>
  • <cstdint>
  • <cstdlib>
  • <ctime>
  • <cwctype>
  • <expected>
  • <format>
  • <initializer_list>
  • <latch>
  • <limits>
  • <new>
  • <numbers>
  • <ranges>
  • <semaphore>
  • <source_location>
  • <span>
  • <spanstream>
  • <stacktrace>
  • <stdfloat>
  • <stop_token>
  • <syncstream>
  • <typeindex>
  • <typeinfo>
  • <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 remove name, and it does not bring this one in - that name gets its own section below.

The second shape is the failure with a suggestion, and it is this sweep's largest surprise: 38 headers fail that way, and no suggestion ever names the algorithm. In 28 of them - among them <vector>, <numeric>, <array>, <list>, <map>, and <utility> - the offered name is move:

Output
-std=c++17 s_vector.cpp -o s_vector_prog
s_vector.cpp: In function 'int main()':
s_vector.cpp:4:19: error: 'remove' is not a member of 'std'; did you mean 'move'?
    4 |     int* e = std::remove(a, a + 5, 1);
      |                   ^~~~~~
      |                   move

std::move is the cast that marks a value as movable; it removes nothing. In 6 headers - <bit>, <charconv>, <chrono>, <coroutine>, <ratio>, and <type_traits> - the offered name is remove_cv, a type trait that strips top-level const and volatile qualifiers from a type:

Output
-std=c++17 s_type_traits.cpp -o s_type_traits_prog
s_type_traits.cpp: In function 'int main()':
s_type_traits.cpp:4:19: error: 'remove' is not a member of 'std'; did you mean 'remove_cv'?
    4 |     int* e = std::remove(a, a + 5, 1);
      |                   ^~~~~~
      |                   remove_cv

The remaining suggestions are C memory functions: <cstring> offers memmove, and <cuchar>, <cwchar>, and <iosfwd> offer wmemmove. Every one of the 38 suggestions is a name that happens to be visible and spelled near remove; following any of them renames the call to a function that does something else entirely.

The third shape is the conversion failure from the previous section: in these 22 headers the file-removing std::remove is visible - pulled in through <cstdio> or through the stream and string machinery that includes it - so the name is a member of std in those files, and the error is cannot convert 'int*' to 'const char*' with a note pointing at the filename overload's declaration:

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

These 22 are the only failing contexts in the sweep that carry note lines at all, and the note never points at <algorithm> - it points at the namesake's declaration in the C header. No failing header in any shape produced a help line.

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, the smallest in this family so far. 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:

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {1, 2, 1, 3, 1};
    auto e = std::remove(v.begin(), v.end(), 1);
    std::cout << (e - v.begin()) << " " << v[0] << " " << v[1] << " " << v.size() << "\n";
    return 0;
}
Terminal
-std=c++17 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
2 2 3 5

One include, <algorithm>, declares the function - and the textbook program prints 2 2 3 5: the new logical end at index 2, the kept elements 2 and 3 in front of it, 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 conversion shape - the same one include is the whole fix.

What the call does: it compacts, it does not shrink

The call returns one iterator, and what it does to the range is where this name's one real subtlety lives. The kept elements are shifted to the front in their original order; the returned iterator marks the new logical end; the container's size does not change; a value that is absent changes nothing; removing everything returns the range's own beginning; and an empty range returns its beginning too:

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {1, 2, 1, 3, 1};
    auto e = std::remove(v.begin(), v.end(), 1);
    std::cout << "kept " << (e - v.begin()) << " " << v[0] << " " << v[1] << "\n";
    std::cout << "size " << v.size() << "\n";
    std::vector<int> w = {3, 1, 4, 1, 5};
    auto e2 = std::remove(w.begin(), w.end(), 9);
    std::cout << "absent " << (e2 - w.begin()) << " " << (e2 == w.end()) << "\n";
    std::vector<int> x = {7, 7, 7};
    auto e3 = std::remove(x.begin(), x.end(), 7);
    std::cout << "all " << (e3 - x.begin()) << " " << (e3 == x.begin()) << "\n";
    std::vector<int> mt;
    auto e4 = std::remove(mt.begin(), mt.end(), 1);
    std::cout << "empty " << (e4 == mt.begin()) << "\n";
    std::vector<int> s = {5, 1, 5, 2, 5, 3};
    auto e5 = std::remove(s.begin(), s.end(), 5);
    std::cout << "stable " << s[0] << " " << s[1] << " " << s[2] << " " << (e5 - s.begin()) << "\n";
    return 0;
}
Terminal
-std=c++17 r_semantics.cpp -o r_semantics_prog
$ ./r_semantics_prog
kept 2 2 3
size 5
absent 5 1
all 0 1
empty 1
stable 1 2 3 3

Read in order: the kept line reports the new logical end at distance 2, with 2 and 3 - the elements that are not 1, in their original order - sitting in front of it. The size line reports 5, and it is the line to remember: nothing was erased, so the vector is exactly as long as before the call, and the three positions from the returned iterator to the old end hold values the standard leaves unspecified - this program never prints them, and no program should read them as data. The absent line reports distance 5 and 1: removing a value that is not there returns the range's own end, untouched. The all line reports 0 and 1: when every element matches, the new logical end is the beginning itself. The empty line reports 1: an empty range's new end is its beginning. The stable line reports 1 2 3 and distance 3: the survivors of 5, 1, 5, 2, 5, 3 keep the order they had, 1 before 2 before 3 - the removal is stable.

Finishing the job: the erase step

Because an algorithm works on iterators alone, it cannot shorten a container it cannot see - the shortening is a second, separate call. Pairing the returned iterator with the container's own erase member is the erase-remove idiom, and it is the standard way this call is used on a container:

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {1, 2, 1, 3, 1};
    v.erase(std::remove(v.begin(), v.end(), 1), v.end());
    std::cout << "size " << v.size() << " " << v[0] << " " << v[1] << "\n";
    return 0;
}
Terminal
-std=c++17 r_erase.cpp -o r_erase_prog
$ ./r_erase_prog
size 2 2 3

The size line now reports 2: the erase call destroyed the unspecified tail and shrank the vector to its logical length, leaving 2 and 3. Since C++20 the two steps also come as one free function for the sequence containers - std::erase takes the container and the value, does both steps, and returns how many elements it removed:

Terminal
-std=c++20 r_erasefree20.cpp -o r_erasefree20_prog
$ ./r_erasefree20_prog
erased 3 size 2 2 3

It reports erased 3 size 2 2 3 - the same end state, with the count of removed elements as part of the answer. That free function is declared by the container's own header, not by <algorithm>; it is the modern shortcut, not a replacement for knowing what the algorithm half does.

Forward iterators: the weakest iterators it accepts

The call needs forward iterators - and writable ones, because it shifts 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 compacts a list the same way it compacts a vector:

C++
#include <algorithm>
#include <iostream>
#include <list>
int main() {
    std::list<int> l = {1, 2, 1, 3, 1};
    auto e = std::remove(l.begin(), l.end(), 1);
    int n = 0;
    for (auto it = l.begin(); it != e; ++it) ++n;
    std::cout << n << " " << l.front() << " " << l.size() << "\n";
    return 0;
}
Terminal
-std=c++17 r_list.cpp -o r_list_prog
$ ./r_list_prog
2 2 5

It reports 2 2 5 - two elements in front of the new logical end, the first of them 2, and the list's size still 5, waiting for the same erase step (a list also offers its own remove member, which erases as it goes - a different function from this algorithm, reached through the container, not through <algorithm>). 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::remove 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 removal compiled as C++98 compiles and runs:

C++
#include <algorithm>
#include <iostream>
int main() {
    int a[5] = {1, 2, 1, 3, 1};
    int* e = std::remove(a, a + 5, 1);
    std::cout << (e - a) << " " << a[0] << " " << a[1] << "\n";
    return 0;
}
Terminal
-std=c++98 c_version98.cpp -o c_version98_prog
$ ./c_version98_prog
2 2 3

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:

C++
#include <algorithm>
#include <vector>
int main() {
    std::vector<int> v = {1, 2, 1, 3, 1};
    auto e = std::remove(std::execution::seq, v.begin(), v.end(), 1);
    return e == v.begin() + 2 ? 0 : 1;
}
Output
-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:31: error: 'std::execution' has not been declared
    5 |     auto e = std::remove(std::execution::seq, v.begin(), v.end(), 1);
      |                               ^~~~~~~~~

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::remove exists:

Terminal
-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:

Output
-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:31: error: 'std::execution' has not been declared
    6 |     auto e = std::remove(std::execution::seq, v.begin(), v.end(), 1);
      |                               ^~~~~~~~~

And since C++20 the classic call is also usable in constant expressions - this program's removal happens at compile time, and the assertion on the result is part of the build:

C++
#include <algorithm>
#include <array>
constexpr int check() {
    std::array<int, 5> a{1, 2, 1, 3, 1};
    auto e = std::remove(a.begin(), a.end(), 1);
    return (e - a.begin() == 2 && a[0] == 2 && a[1] == 3) ? 1 : 0;
}
static_assert(check());
int main() { return 0; }
Terminal
-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:

Output
-std=c++17 c_constexpr17.cpp -o c_constexpr17_prog
c_constexpr17.cpp: In function 'constexpr int check()':
c_constexpr17.cpp:5:25: warning: call to non-'constexpr' function '_FIter std::remove(_FIter, _FIter, const _Tp&) [with _FIter = int*; _Tp = int]' [-Winvalid-constexpr]
    5 |     auto e = std::remove(a.begin(), a.end(), 1);
      |              ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/13/algorithm:61,
                 from c_constexpr17.cpp:1:
/usr/include/c++/13/bits/stl_algo.h:836:5: note: '_FIter std::remove(_FIter, _FIter, const _Tp&) [with _FIter = int*; _Tp = int]' declared here
  836 |     remove(_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: none of them share a side door

The three names a reader most often reaches for alongside this one all live in <algorithm> too, and in a file whose only include is <vector> they all fail - 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. They do not even fail alike. std::remove_if - which removes the elements a predicate accepts - fails with a suggestion, and the suggestion names remove_cv, the type trait:

Output
-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

std::unique - which compacts away consecutive duplicates - and std::replace - which overwrites every element equal to one value with another - fail in the same file with the plain error:

Output
-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());
      |                   ^~~~~~
Output
-std=c++17 b_replace_vector.cpp -o b_replace_vector_prog
b_replace_vector.cpp: In function 'int main()':
b_replace_vector.cpp:4:10: error: 'replace' is not a member of 'std'
    4 |     std::replace(v.begin(), v.end(), 1, 9);
      |          ^~~~~~~

Three neighbors, one home by the standard, and one fate in this library: all three are left outside the rescue set, in two different diagnostic shapes. They are shown here only as neighbors, not re-taught.

The neighboring names, fixed

Together the four 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 four operations:

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {1, 2, 1, 3, 1};
    auto e = std::remove(v.begin(), v.end(), 1);
    std::cout << "remove";
    for (auto it = v.begin(); it != e; ++it) std::cout << " " << *it;
    std::cout << "\n";
    std::vector<int> w = {1, 2, 3, 4, 5, 6};
    auto e2 = std::remove_if(w.begin(), w.end(), [](int x) { return x % 2 == 0; });
    std::cout << "remove_if";
    for (auto it = w.begin(); it != e2; ++it) std::cout << " " << *it;
    std::cout << "\n";
    std::vector<int> x = {1, 1, 2, 2, 3};
    auto e3 = std::unique(x.begin(), x.end());
    std::cout << "unique";
    for (auto it = x.begin(); it != e3; ++it) std::cout << " " << *it;
    std::cout << "\n";
    std::vector<int> y = {1, 2, 1, 3};
    std::replace(y.begin(), y.end(), 1, 9);
    std::cout << "replace";
    for (int n : y) std::cout << " " << n;
    std::cout << "\n";
    return 0;
}
Terminal
-std=c++17 b_neighbors_fixed.cpp -o b_neighbors_fixed_prog
$ ./b_neighbors_fixed_prog
remove 2 3
remove_if 1 3 5
unique 1 2 3
replace 9 2 9 3

Read in order: remove leaves 2, 3 in front of the new logical end; remove_if, under a predicate that accepts the even values, leaves 1, 3, 5; unique leaves 1, 2, 3 in front of its new logical end, the consecutive duplicates compacted away; and replace has overwritten every 1 in its range with 9, in place, with no end returned because nothing was compacted.

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::remove is a function object in the ranges namespace, declared in <algorithm>, that accepts a whole range and returns a subrange whose begin is the new logical end and whose end is the range's old end - the kept part and the unspecified tail, split at the returned point. Compiled as C++20 it compacts the same vector and reports the same shape of answer:

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {1, 2, 1, 3, 1};
    auto r = std::ranges::remove(v, 1);
    std::cout << "ranges " << (r.begin() - v.begin()) << " " << v[0] << " " << v[1] << " " << v.size() << "\n";
    return 0;
}
Terminal
-std=c++20 r_ranges.cpp -o r_ranges_prog
$ ./r_ranges_prog
ranges 2 2 3 5

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:

Output
-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::remove(v, 1);
      |                   ^~~~~~

And in this library the ranges header does not rescue the ranges form - a file whose only include is <array> fails for it with the plain error, and a file whose only include is <ranges> fails for it the same way, its transcript differing only in extra note lines that offer the global C remove as a suggested alternative:

Output
-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: 'remove' is not a member of 'std::ranges'
    4 |     auto r = std::ranges::remove(a, 1);
      |                           ^~~~~~

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::remove 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:

C++
#include <algorithm>
int main() {
    int a[5] = {1, 2, 1, 3, 1};
    remove(a, a + 5, 1);
    return 0;
}
Output
-std=c++17 g_unqualified.cpp -o g_unqualified_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:4:5: error: 'remove' was not declared in this scope; did you mean 'std::remove'?
    4 |     remove(a, a + 5, 1);
      |     ^~~~~~
      |     std::remove
In file included from /usr/include/c++/13/algorithm:73,
                 from g_unqualified.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:224:1: note: 'std::remove' declared here
  224 | remove(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value);
      | ^~~~~~

That wording - was not declared in this scope, with did you mean 'std::remove'? 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::remove; 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:

C++
#include <algorithm>
#include <vector>
int main() {
    std::vector<int> v = {1, 2, 1, 3, 1};
    return remove(v.begin(), v.end(), 1) == v.begin() + 2 ? 0 : 1;
}
Terminal
-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::remove through them. A list's iterators rescue it the same way - the same unqualified call over a list compiles and runs:

Terminal
-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:

C++
#include <algorithm>
#include <array>
int main() {
    std::array<int, 5> a{1, 2, 1, 3, 1};
    return remove(a.begin(), a.end(), 1) == a.begin() + 2 ? 0 : 1;
}
Output
-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:12: error: 'remove' was not declared in this scope; did you mean 'std::remove'?
    5 |     return remove(a.begin(), a.end(), 1) == a.begin() + 2 ? 0 : 1;
      |            ^~~~~~
      |            std::remove
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:224:1: note: 'std::remove' declared here
  224 | remove(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __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::remove is 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.
  • 'remove' 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 three shapes in the sweep probed here: 41 failing sole headers fail with one error line and no suggestion tail, no note lines, and no help lines; 38 fail with a suggestion, and every suggestion names a wrong name - move in 28 files, remove_cv in 6, wmemmove in 3, and memmove in 1; and 22 fail with cannot convert 'int*' to 'const char*' instead, because the file-deleting std::remove from <cstdio> is visible in those files and the call is measured against it, with a note pointing at that function's declaration.
  • In the sweep probed here, 4 headers besides <algorithm> rescue std::remove 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::remove in.
  • The result rules are the documented ones, as the fixed programs here show: the fixed program prints 2 2 3 5 for the textbook case; the semantics program prints kept 2 2 3, size 5, absent 5 1, all 0 1, empty 1, and stable 1 2 3 3 - the call shifts the elements that are not equal to the value to the front in their original order, returns an iterator past the new logical end, and leaves the container's size unchanged, with the elements from the returned iterator to the old end holding unspecified values.
  • The shortening step follows from the same rule: the erase-remove pairing prints size 2 2 3, because the container's erase member destroys the tail the algorithm could not; and since C++20 the free std::erase for a vector does both steps in one call, printing erased 3 size 2 2 3 here.
  • The classic call's iterators must be forward iterators, and writable: the list program here compacts a list and prints 2 2 5.
  • The classic std::remove predates C++11: with <algorithm> included it compiles and runs as C++98, reporting 2 2 3. The layered pieces have floors: the execution-policy overload starts at C++17 - with only <algorithm> included, naming std::execution::seq fails 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 share this name's fate in this library, not its side door - it has none worth the name: std::remove_if, std::unique, and std::replace are declared by #include <algorithm> as well and all fail in a sole <vector> file, remove_if with the suggestion did you mean 'remove_cv'? and the other two with the plain error. The fixed neighbor program prints remove 2 3, remove_if 1 3 5, unique 1 2 3, and replace 9 2 9 3.
  • std::ranges::remove is a ranges form of this same name: a function object in namespace std::ranges, declared in <algorithm> by the standard, needing C++20, returning a subrange whose begin is the new logical end - compiled as C++17 the ranges program here fails with 'std::ranges' has not been declared, and compiled as C++20 it prints ranges 2 2 3 5 for 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, with extra note lines offering the global C remove, <array> alone fails for it the same way, and <ranges> fails plainly for the classic name in the sweep.
  • 'remove' was not declared in this scope for an unqualified remove use is a different defect: qualify the name as std::remove - the include alone does not bring the name out of namespace std. 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 are std class types; over a std::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::remove. 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 - 41 of the 101 probed failures did - with a suggestion, or with the conversion error, 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 any suggestion this error offers here: in the probed set the compiler proposes move, remove_cv, wmemmove, and memmove, and every one of them is a different operation - a movability cast, a type trait, and two memory-copy functions. Renaming the call to any of them does not remove elements from a range. The fix for those files is the same include as everywhere else.
  • Third, when the error mentions const char*, recognize the namesake: the file can see the file-deleting std::remove from <cstdio> and cannot see the algorithm. Do not change the call's arguments to suit that function - add <algorithm>, and overload resolution picks the iterator form.
  • Fourth, 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.
  • Fifth, 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.
  • Sixth, 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.
  • Seventh, do not expect the call to shorten the container: it compacts and returns the new logical end, and the size stays put until the returned iterator is paired with the container's erase - or, at C++20, until the free std::erase does both steps.
  • Eighth, if the error names std::execution instead - '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.
  • Ninth, 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::remove needs C++20 - with its header still <algorithm> by the standard, not <ranges>.
  • Tenth, if the error says was not declared in this scope instead, fix the name's qualifier to std::remove as well - and do not rely on argument-dependent lookup to rescue the unqualified call, because a plain array and a std::array each remove that rescue in the probed set.

Sources

  • cppreference, std::remove, std::remove_if - the function templates are defined in <algorithm>; remove shifts the elements that are not equal to the given value to the front and returns an iterator past the new logical end, the physical size of the range is unchanged, and the elements past the new end hold valid but unspecified values; the execution-policy overloads are marked since C++17 and the classic overload constexpr since C++20, with no since marker on the classic call itself; the page also notes the unrelated std::remove overload taking a const char*, used to delete files.
  • cppreference, <algorithm> header - the header that declares this name and the other algorithm names used as neighbors here.
  • cppreference, std::ranges::remove, std::ranges::remove_if - the C++20 ranges forms of this name, function objects in namespace std::ranges declared in <algorithm>, returning a subrange split at the new logical end.
  • Wikipedia, Erase-remove idiom - the pairing of this algorithm with a container's erase member that actually removes the tail, and the C++20 free std::erase and std::erase_if functions that perform both steps.

Keep Exploring