Answers for "c++ remove element from vector"


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);
Posted by: Guest on February-19-2020

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());
Posted by: Guest on February-23-2020

c++ remove element from vector

vector.erase(position) // remove certain position
// or
vector.erase(left,right) // remove positions within range
Posted by: Guest on May-26-2020

Code answers related to "c++ remove element from vector"

Browse Popular Code Answers by Language