How to implement pop_front to a vector in C++
In this tutorial, we will see how to implement pop_front to a vector in C++.
Before we proceed we should know some important terms, so what exactly vector is?
Vector is a sequence container and we also call it as dynamic array because in the array we can’t add or remove elements but in vector, we can add and remove the last element of a vector by using push_back()
& pop_back()
functions. The vector contains some built-in functions but the vector does not have a pop_front function. As the name suggests pop_front
means to remove the first element from a vector. Here we have to create our own pop_front
function to remove the first element from a vector. There are some approaches let’s see them through examples one by one.
Example 1 –
#include<iostream> #include<vector> using namespace std; void pop_front(vector<int>& v){ if(v.size() == 0){ cout<<"Vector is empty"<<endl; } //erase() is built in function in vector which removes element else{ v.erase(v.begin()); } } void display(vector<int>& v){ for(int i = 0;i<v.size();i++){ cout<<v[i]<<" "; } cout<<endl; } int main(){ vector<int>vec1 = {1,2,3,4,5,6,7,8,9,10}; cout<<"Elements before calling pop_front function are: "<<endl; display(vec1); pop_front(vec1); cout<<"Elements after calling pop_front function are: "<<endl; display(vec1); return 0; }
Output –
Elements before calling pop_front function are: 1 2 3 4 5 6 7 8 9 10 Elements after calling pop_front function are: 2 3 4 5 6 7 8 9 10
In the first example, we include a vector header file which contains common built-in functions of a vector. Then we created two functions where the display function is used to display all the elements of a vector and the pop_front
function is used to remove the first element of a vector. In the pop_front
function, we use the erase function which is used to remove the particular element of the vector and in that function, we specify the begin function. Here the begin function points to the first element of the vector. So this way we can remove the first element of the vector through the pop_front
function easily.
Example 2 –
#include<iostream> #include<bits/stdc++.h> // #include<algorithm> // #include<vector> using namespace std; void pop_front(vector<int>& v){ reverse(v.begin(),v.end()); v.pop_back(); reverse(v.begin(),v.end()); } void display(vector<int>& v){ for(int i = 0;i<v.size();i++){ cout<<v[i]<<" "; } cout<<endl; } int main(){ vector<int>vec1 = {1,2,3,4,5,6,7,8,9,10}; cout<<"Elements before calling pop_front function are: "<<endl; display(vec1); pop_front(vec1); cout<<"Elements after calling pop_front function are: "<<endl; display(vec1); return 0; }
Output –
Elements before calling pop_front function are: 1 2 3 4 5 6 7 8 9 10 Elements after calling pop_front function are: 2 3 4 5 6 7 8 9 10
In the second example, the overall code is the same but the logic inside the pop_front
function is different. In the pop_front function, we have used the reverse function but the reverse function is not present in the vector header file so that’s why we need to include the algorithm header file or bits/stdc++.h header file.
The reverse function is used to reverse the elements. After reversing the elements the first element will move to the last position and then we have a pop_back
built-in function that is used to remove that element. Again we used the reverse function to arrange all the elements back to the previous positions. So these two are the common approaches to removing the first element of the vector. Hope this tutorial will be helpful.
Leave a Reply