remove the first element of a vector c++ code example
Example 1: c++ vector pop first element
std::vector<int> vect;
vect.erase(vect.begin());
Example 2: remove element by index from vector c++
vec.erase(vec.begin() + 1);
vec.erase(vec.begin() + 1, vec.begin() + 3);
Example 3: delete from front in vector c++
vector_name.erase(vector_name.begin());
vector_name.erase(vector_name.begin()+(x-1));
vector_name.pop_back();
Example 4: remove element by index from vector c++
auto removeByIndex =
[]<class T>(std::vector<T> &vec, unsigned int index)
{
vec.erase(vec.begin() + index);
};
std::vector<std::string> stringvec = {"Hello", "World"};
removeByIndex(stringvec, 1);
std::vector<unsigned int> intvec;
intvec.push_back(33);
intvec.push_back(66);
intvec.push_back(99);
removeByIndex(intvec, 2);