pop_front vector c++ code example

Example 1: c++ vector pop first element

std::vector<int> vect;

vect.erase(vect.begin());

Example 2: c++ vector pop_back

#include <bits/stdc++.h> 
using namespace std;
int main(){
    vector<int> v1{10, 20, 30, 40, 50};
    
    //removing elemenets
    v1.pop_back();  //removes 50
    v1.pop_back();  //removes 40

}

Example 3: delete from front in vector c++

// Deleting first element
vector_name.erase(vector_name.begin());

// Deleting xth element from start
vector_name.erase(vector_name.begin()+(x-1));

// Deleting from the last
vector_name.pop_back();

Example 4: c++ list pop back

void pop_back();
// desc: remove last element. complexity: constant

Tags:

Cpp Example