The list::pop_back() is a built-in function in C++ STL which is used to remove an element from the back of a list container. That is, this function deletes the last element of a list container. This function thus decreases the size of the container by 1 as it deletes an element from the end of the list.
Syntax
list_name.pop_back();
Parameters
- The function does not accept any parameter.
Return Value
- This function does not return anything.
Example
The below program illustrates the list::pop_back() function in C++ STL.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
list< int > demoList;
demoList.push_back(10);
demoList.push_back(20);
demoList.push_back(30);
demoList.push_back(40);
cout << "Initial List: " ;
for ( auto itr = demoList.begin(); itr != demoList.end();
itr++)
cout << *itr << " " ;
demoList.pop_back();
cout << "\n\nList after removing an element from end: " ;
for ( auto itr = demoList.begin(); itr != demoList.end();
itr++)
cout << *itr << " " ;
return 0;
}
|
Output
Initial List: 10 20 30 40
List after removing an element from end: 10 20 30
Time Complexity: O(1)
Auxiliary Space: O(1)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
30 May, 2023
Like Article
Save Article