erase function in c++ code example

Example 1: remove element by index from vector c++

// Deletes the second element (vec[1])
vec.erase(vec.begin() + 1);

// Deletes the second through third elements (vec[1], vec[2])
vec.erase(vec.begin() + 1, vec.begin() + 3);

Example 2: remove or erase first and last character of string c++

str.pop_back(); // removes last /back character from str
str.erase(str.begin()); // removes first/front character from str

Example 3: c++ erase remove

std::vector<int> v = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
v.erase(std::remove(v.begin(), v.end(), 5), v.end());
// v will be {0 1 2 3 4 6 7 8 9}