list pop_back() function in C++ STL

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 list.

Syntax:

list_name.pop_back();

Parameters: The function does not accept any parameter.

Return Value: This function does not returns anything.

Below program illustrate the list::pop_back() function in C++ STL:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate the
// list::pop_back() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Creating a list
    list<int> demoList;
  
    // Adding elements to the list
    // using push_back()
    demoList.push_back(10);
    demoList.push_back(20);
    demoList.push_back(30);
    demoList.push_back(40);
  
    // Initial List:
    cout << "Initial List: ";
    for (auto itr = demoList.begin(); itr != demoList.end(); itr++)
        cout << *itr << " ";
  
    // removing an element from the end of List
    // using pop_back
    demoList.pop_back();
  
    // List after removing element from end
    cout << "\n\nList after removing an element from end: ";
    for (auto itr = demoList.begin(); itr != demoList.end(); itr++)
        cout << *itr << " ";
  
    return 0;
}

chevron_right


Output:

Initial List: 10 20 30 40 

List after removing an element from end: 10 20 30


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.