1// Deletes the second element (vec[1])
2vec.erase(vec.begin() + 1);
3
4// Deletes the second through third elements (vec[1], vec[2])
5vec.erase(vec.begin() + 1, vec.begin() + 3);
1#include <algorithm>
2#include <vector>
3
4// using the erase-remove idiom
5
6std::vector<int> vec {2, 4, 6, 8};
7int value = 8 // value to be removed
8vec.erase(std::remove(vec.begin(), vec.end(), value), vec.end());
1vector.erase(position) // remove certain position
2// or
3vector.erase(left,right) // remove positions within range
4
1// Why not setup a lambda you can use again & again
2auto removeByIndex =
3 []<class T>(std::vector<T> &vec, unsigned int index)
4{
5 // This is the meat & potatoes
6 vec.erase(vec.begin() + index);
7};
8
9// Then you can throw whatever vector at it you desire
10std::vector<std::string> stringvec = {"Hello", "World"};
11// Will remove index 1: "World"
12removeByIndex(stringvec, 1);
13// Vector of integers, we will use push_back
14std::vector<unsigned int> intvec;
15intvec.push_back(33);
16intvec.push_back(66);
17intvec.push_back(99);
18// Will remove index 2: 99
19removeByIndex(intvec, 2);