Lists are sequence containers available in the C++ Standard Library. They allow non-contiguous memory allocation. List containers are implemented as doubly-linked lists. Compared to other sequence containers (eg: arrays, vectors etc.), lists have a better performance when it comes to inserting, moving or deleting elements. The list::pop_back()
is a method available in the STL which deletes the last element of the list. It effectively decreases the container size by one.
Syntax:
list_name.pop_back();
Parameters: The list::pop_back()
does not accept any parameter:
Return value: none
Example of list::pop_back() method
#include <iostream> #include <list> using namespace std; int main() { list<int> L; L.push_back(5); L.push_back(10); L.push_back(15); L.push_back(20); cout<<"List before pop: \n"; for( list<int>::iterator it = L.begin(); it != L.end(); ++it) { cout<<*it<<" <--> "; } cout<<"NULL"; cout<<endl; L.pop_back(); cout<<"List after pop: \n"; for( list<int>::iterator it = L.begin(); it != L.end(); ++it) { cout<<*it<<" <--> "; } cout<<"NULL"; return 0; }
Output:
List before pop: 5 <--> 10 <--> 15 <--> 20 <--> NULL List after pop: 5 <--> 10 <--> 15 <--> NULL