pop an element from vector c++ code example
Example 1: c++ vector pop first element
std::vector<int> vect;
vect.erase(vect.begin());
Example 2: vector pop back
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector;
int sum (0);
myvector.push_back (100);
myvector.push_back (200);
myvector.push_back (300);
while (!myvector.empty())
{
sum+=myvector.back();
myvector.pop_back();
}
std::cout << "The elements of myvector add up to " << sum << '\n';
return 0;
}
Example 3: vector erase specific element
vector.erase( vector.begin() + 3 );
Example 4: c++ remove element from vector
vector.erase(position)
vector.erase(left,right)