vector delete element by value code example
Example 1: 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);
}
Example 2: how to remove an element from a vector by value c++
std::vector<int> v;
v.erase(std::remove(v.begin(), v.end(), 99), v.end());
Example 3: c++ vector remove element by value
carVec.erase(std::remove_if(carVec.begin(), carVec.end(), [&id_to_delete](const Car& ele)->bool
{
return ele.getnewId() == id_to_delete;
}), carVec.end());