erase from vector by value cpp code example
Example 1: remove value from vector c++
#include <algorithm>
#include <vector>
std::vector<int> vec {2, 4, 6, 8};
int value = 8
vec.erase(std::remove(vec.begin(), vec.end(), value), vec.end());
Example 2: c++ vector erase by value
#include <algorithm>
#include <iostream>
#include <vector>
void Print(const std::vector<int>& vec) {
for (const auto& i : vec) {
std::cout << i << ' ';
}
std::cout << '\n';
}
int main() {
std::vector<int> v = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Print(v);
v.erase(std::remove(v.begin(), v.end(), 5), v.end());
Print(v);
}