#include <algorithm>
#include <vector>
// using the erase-remove idiom
std::vector<int> vec {2, 4, 6, 8};
int value = 8 // value to be removed
vec.erase(std::remove(vec.begin(), vec.end(), value), vec.end());
std::vector<int> v;
// fill it up somehow
v.erase(std::remove(v.begin(), v.end(), 99), v.end());
// really remove all elements with value 99
#include <vector>
#include <algorithm>
#include <iostream>
int main()
{
//C++20 std::erase function wraps erase-remove idiom
std::vector<int> ivec{ 1, 5, 7, 2, 1, 3, 1, 7, 1 };
auto n = erase(ivec, 1); //ADL
std::cout << n << " elements erased
";
for (auto i : ivec)
std::cout << i << ' ';
}
#include <algorithm>
...
vec.erase(std::remove(vec.begin(), vec.end(), 8), vec.end());
carVec.erase(std::remove_if(carVec.begin(), carVec.end(), [&id_to_delete](const Car& ele)->bool
{
return ele.getnewId() == id_to_delete;
}), carVec.end());
Code Example |
---|
Cpp :: middle node of linked list |
Cpp :: fenwick tree |
Cpp :: C++ area & circumference of a circle |
Cpp :: how a function gives a pointer as parameter c++ |
Cpp :: how to create a structure c++ |
Cpp :: transform cpp |
Cpp :: c++ for |
Cpp :: how to replace an element in array in c++ |
Cpp :: Arduino Counting |
Cpp :: insert into a vector more than once c++ |
Cpp :: set elements to 42 back |
Cpp :: c++ short hand if else |
C :: hello word c |
C :: clear screen c |
C :: how to download file in powershell |
C :: arduino serial read write structure |
C :: c program for threaded binary tree |
C :: curl authorization header |
C :: random number c |
C :: Succ de ch |
C :: dynamically create matrix c |
C :: servo motor arduino |
C :: recursion to convert decimal to binary |
C :: c programing strtok use |
C :: strong number in c |
C :: set value of boolean in c |
C :: read from stdin c |
C :: struct main function c in unix |
C :: simple bootstrap form example |
C :: concatenate two strings without standard library in C |