#include#include int main() { std::vector v{1, 5, 3, 5, 2, 5, 4}; v.erase(std::remove(v.begin(), v.end(), 5), v.end()); // after this call, v contains {1, 3, 2, 4} only return 0; }
#include#include int main() { std::vector v{1, 2, 3, 4, 5, 6}; v.erase(std::remove_if(v.begin(), v.end(), [](int x) { return x % 2; }), v.end()); // after this call, v contains {2, 4, 6} only return 0; }
#includeThe `std::vector::remove()` function is part of the C++ Standard Library.#include struct Person { std::string name; int age; }; int main() { std::vector v{{"Alice", 20}, {"Bob", 30}, {"Charlie", 30}, {"David", 40}}; v.erase(std::remove_if(v.begin(), v.end(), [](Person& p) { return p.age == 30; }), v.end()); // after this call, v contains {{"Alice", 20}, {"David", 40}} only return 0; }