Open In App

How to Remove an Item from STL Vector with a Certain Value?

Last Updated : 03 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn the different methods to remove items with a certain value from vector in C++.

The simplest method to remove all items with specific value from a vector is by using remove() method with vector erase(). Let’s take a look at an example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {1, 2, 2, 3, 2, 5};

    // Remove all occurences of 2
    v.erase(remove(v.begin(), v.end(), 2), v.end());

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
1 3 5 

Explanation: In the above code, the remove() method shifts all elements other than 2 to the front of the vector and returns an iterator to the new logical end, excluding 2. The erase() function is then used to remove the extra elements from the new logical end to the original end of the vector.

There are also some other methods in C++ to remove all occurrence of specific value from a vector. Some of them are as follows:

Using find() and Vector erase()

Repeatedly find the iterator to the specific value from vector using find() and then remove that element by specifying the iterator using vector erase() till there are no instances of that value left in the vector.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {1, 2, 2, 3, 2, 5};

    // Remove all occurrence of 2
    auto it = find(v.begin(), v.end(), 2);
    while (it != v.end()) {
        v.erase(it);
        it = find(v.begin(), v.end(), 2);
    }

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
1 3 5 

Manually Using Loop

To remove all occurrences of a specific value from a vector, iterate through the vector and check each element. If an element matches the specified value, remove it from the vector using vector erase().

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {1, 2, 2, 3, 2, 5};

    // Remove all occurrence of 2
    for (auto i = v.begin(); i != v.end();) {
        if (*i == 2)
            i = v.erase(i);
        else
            i++;
    }

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
1 3 5 

Explanation: In this code, we remove all occurrence of 2 from vector by iterating through it using an iterator. If an element equals 2, it is removed using erase() and the iterator is updated to the next element. If the element is not 2, the iterator simply advances to the next element.

Using erase() (Since C++20)

The erase() introduced in C++ 20 can directly remove all the occurrence of specified value from the vector container.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {1, 2, 2, 3, 2, 5};

    // Remove all occurrence of 2
    erase(v, 2);

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output

1 3 5

Article Tags :
Practice Tags :

Similar Reads