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

In this tutorial, I’ll show you how to pop front in Vector. In C++, you can simply remove or pop the first member in a vector with this method.

The std::vector does not include a function for removing or popping an element from the top of the list, as you may already know. Push back and pop back are the only two functions available for adding and removing components.

This is in accordance with the vector design. The first element must be deleted or erased before the rest of the vector’s elements may be rearranged in their original order to remove it.

After the first element is deleted, it will take n-1 iterations to reorder the vector. Linear time execution O(n) is another term for this. Let’s get started by implementing the pop front function, which takes linear time, and then we’ll think about how to optimize it better.

This problem can be further optimized and can be solved in constant runtime of O(1).

1. Using erase() To Pop Front in Vector [pop_front Method] – Naive Approach

To begin, let’s have a look at the linear runtime technique, which is also known as the naive approach to this problem statement. You can use the inbuilt function erase() to delete the front or top element of the vector.

#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 are not having a large number of lists or vectors from which you want to remove or pop the front element then this solution can also work for you.

2. Using Deque To Pop Front In Vector – Optimized Approach

A better strategy is available. Deque can be used in place of the vector. The list of elements in Deque has the same functionality as a vector, but you can additionally add and remove elements from the rear and front of the list 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:

Before Pop Func: 1 2 3 4 6
After Pop Front: 2 3 4 6
After Pop Front: 3 4 6
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.

3. Using pop_back() To Pop Front In Vector – If Order Of Element Does Not Matter

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

Wrap Up

I hope you were able to solve the pop front vector in the C++ problem using the three methods listed above by me. Let me know if you have any better idea than the one discussed above I will be happy to add it here.

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.

Further Read:

  1. 2D Vector Implementation in C++ Complete Guide
  2. How To Check If Vector Contains A Given Element in C++
  3. How To Declare An Empty Array in C++
  4. Iterate Through Character Of String in C++
  5. Check If String is Palindrome Using Recursion

2 thoughts on “How To Pop Front in Vector C++ [pop_front]”

Leave a Comment