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// Deleting first element
2vector_name.erase(vector_name.begin());
3
4// Deleting xth element from start
5vector_name.erase(vector_name.begin()+(x-1));
6
7// Deleting from the last
8vector_name.pop_back();
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);