delete from a vector code example

Example 1: remove value from vector c++

#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());

Example 2: vector erase specific element

vector.erase( vector.begin() + 3 ); // Deleting the fourth element

Example 3: c++ remove element from vector

vector.erase(position) // remove certain position
// or
vector.erase(left,right) // remove positions within range