2 minute read

Introduction

The std::remove function is a highly useful function that allows us to remove elements from a container. However, we can often get errors from the compiler such as:

error: ‘remove’ is not a member of ‘std’

This error is pretty straight-forward to fix as we will see below.

Potential causes

The remove is not a member of std error could be caused by multiple reasons.

Fix #1: Add algorithm to your depedencies

Essentially, the std::remove function needs to have access to the <algorithm> module in order to be executed by the compiler.

Therefore, you must add the following #include header to the top of your code (in the include(s) part) such as:

#include <algorithm> //Add this
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5};
    v.erase(std::remove(v.begin(), v.end(), 3), v.end());
    return 0;
}

The compiler should now recognize the std::remove function, thus fixing the remove is not a member of std error.

If the std error still persists, keep reading below.

Fix #2: Using namespace std

Note that we have previously typed: std::remove instead of remove. We can type “remove” only if we are declaring that we are using its namespace.

In other words, we would need to type “using namespace std” in the header if we only want to type remove (which is obviously shorter) instead of std::remove. For instance, we can have something like:

#include <algorithm>
#include <vector>
using namespace std; //Add this

int main() {
    vector<int> v = {1, 2, 3, 4, 5};
    v.erase(remove(v.begin(), v.end(), 3), v.end());
    return 0;
}

It is okay to type std::remove without typing “using namespace std”. In fact, it is generally recommended to type the full std::remove function name (and therefore avoiding using namespace std) when working with multiple libraries because it can reduce future confusion.

However, if you still want to type “remove” instead of “std::remove”, then you need to add “using namespace std” to the header.

References

Read more about the std::remove function here: https://www.cplusplus.com/reference/algorithm/remove/