How To Pop Front in Vector C++ [pop_front]

In this article, I will show you how to pop front in Vector C++. Using this method you will be easily pop or remove the first element of the vector in C++.

As you might already know what std::vector does not have any particular method defined in its class to remove or pop the element from the front of the list. It only contains two functions for the addition and removal of elements namely push_back and pop_back.

This is as per the design for vector. Hence to remove the first element you need to delete or erase the first element and then rearrange all the elements of the vector in its order.

To do so it will take O(n) iteration to put the vector back into its order after the first element is removed. And this is called a linear time execution. Let me implement the pop_front function that runs into linear time and then we can think of what is the alternative that we can use to make it quicker.

Here quicker means how you can deduce this problem to the constant time approach of O(1).

Pop Front in Vector [pop_front Method]

#include<bits/stdc++.h>
using namespace std;

void pop_front(vector<int> &twoDimenVector){

    // Using Erase Function to erase the first Element
    // And Rearranging the other elements of Vector.
    twoDimenVector.erase(twoDimenVector.begin());

}
 
int main(){

    //Initializing vector with some Integer values
    vector<int> newArray{1,2,3,4,6};

    //Deleting the First Element of the Vector
    pop_front(newArray);

    for(int x:newArray)
        cout<<x<<" ";


    return 0;         
}

Output:

2 3 4 6 

Note: This is an inefficient solution and it takes O(n) time to execute. If you really want to use the function to delete the first element of the list you are going to use then you may try this solution.

There is a better approach than this. You may use deque instead of vector. Deque will give you the same features as a vector for the list of elements and also it lets you add in back and pop from the front in constant time. If your application is going to only use pop front then deque is the best choice to go for.

In the below code we see how deque help to remove the pop front issues of the list in c++. It also lets you delete from back and front i.e. you can pop front and pop back in constant time using deque.

#include<bits/stdc++.h>
using namespace std;
 
int main(){

    //Initializing deque with some Integer values
    deque<int> newArray{1,2,3,4,6};

    cout<<"Before Pop Func: ";
    for(int x:newArray)
        cout<<x<<" ";

    cout<<endl;
    //Deleting the First Element of the Vector
    newArray.pop_front();

    cout<<"After Pop Front: ";
    for(int x:newArray)
        cout<<x<<" ";
    cout<<endl;

    //Using the Pop Back Function to Remove element from back.
    newArray.pop_back();
    cout<<"After Pop Front: ";
    for(int x:newArray)
        cout<<x<<" ";

    return 0;         
}

Output:

How To Pop Front in Vector C++ [pop_front]

As you can see in the above code deque resolves the problem of linear time execution of deleting of the first element in the vector.

But if you still want to use vector and you do not care about the index of the element i.e. about the order of the element in vector then you can remove the first element by swapping it by the back of the element and then deleting the back element using the pop_back function of vector in constant time.

In the below code you can see the removal of the first element of the vector if an order does not matter.

#include<bits/stdc++.h>
using namespace std;
 
int main(){

    //Initializing vector with some Integer values
    vector<int> newArray{1,2,3,4,6};

    //Swapping the Front Element with Back of the Element.
    newArray.front() = newArray.back();

    //Popping the last element as that one was first element.
    newArray.pop_back();

    for(int x:newArray)
        cout<<x<<" ";


    return 0;         
}

Output:

6 2 3 4

That is all for this post on how to pop front in Vector C++. Then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Also, subscribe to our newsletter to get notifications for the new posts.

Further Read:

  1. 2D Vector Implementation in C++ Complete Guide
Share your love

One comment

Leave a Reply