cpp std vector remove code example
Example 1: 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 2: remove element by index from vector c++
auto removeByIndex =
[]<class T>(std::vector<T> &vec, unsigned int index)
{
vec.erase(vec.begin() + index);
};
std::vector<std::string> stringvec = {"Hello", "World"};
removeByIndex(stringvec, 1);
std::vector<unsigned int> intvec;
intvec.push_back(33);
intvec.push_back(66);
intvec.push_back(99);
removeByIndex(intvec, 2);