Skip to content
Related Articles

Related Articles

How to delete last element from a List in C++ STL

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 18 Jan, 2019

Given a List, the task is to remove the last element from this List in C++.

Examples:

Input: list = [10 20 30 70 80 90 100 40 50 60]
Output: 10 20 30 40 50 60 70 80 90

Input: list = [1 2 3 4 5]
Output: 1 2 3 4

Lists are a type of associative containers in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the list, though it is possible to remove and add the modified value of that element.

The last element of the List can be deleted by by passing its iterator.

Syntax:

iterator erase (const_iterator positionOfElementToBeDeleted);

Approach: One can easily delete the last element by passing its iterator to the erase function. To reach the iterator which points to the last element, there are two ways:

  1. Method 1:
    prev(listInt.end())

    Below is the implementation of the above approach:

    Program 1:




    // C++ program to delete last element
    // of a List by passing its iterator
      
    #include <iostream>
    #include <list>
    using namespace std;
      
    // Function to print the list
    void printList(list<int> mylist)
    {
      
        // Get the iterator
        list<int>::iterator it;
      
        // printing all the elements of the list
        for (it = mylist.begin(); it != mylist.end(); ++it)
            cout << ' ' << *it;
        cout << '\n';
    }
      
    // Function to delete last element of list
    // using method 1
    void deleteByMethod1(list<int> mylist)
    {
      
        // printing all the elements of the list
        cout << "\nList originally: ";
        printList(mylist);
      
        // Get the iterator
        list<int>::iterator it;
      
        // Get the positionOfElementToBeDeleted
        // using method 1
        it = prev(mylist.end());
      
        // Erase the last element
        // currently pointed by the iterator
        mylist.erase(it);
      
        // printing all the elements of the list
        cout << "List after deletion: ";
        printList(mylist);
    }
      
    // Driver code
    int main()
    {
        list<int> mylist;
      
        // Get the list
        for (int i = 1; i < 10; i++)
            mylist.push_back(i * 10);
      
        // Method 1 to get positionOfElementToBeDeleted
        deleteByMethod1(mylist);
      
        return 0;
    }

    Output:

    List originally:  10 20 30 40 50 60 70 80 90
    List after deletion:  10 20 30 40 50 60 70 80
    
  2. Method 2:
    list::iterator it = listInt.end(); 
    --it;
    

    Below is the implementation of the above approach:

    Program 2:




    // C++ program to delete last element
    // of a List by passing its iterator
      
    #include <iostream>
    #include <list>
    using namespace std;
      
    // Function to print the list
    void printList(list<int> mylist)
    {
      
        // Get the iterator
        list<int>::iterator it;
      
        // printing all the elements of the list
        for (it = mylist.begin(); it != mylist.end(); ++it)
            cout << ' ' << *it;
        cout << '\n';
    }
      
    // Function to delete last element of list
    // using method 2
    void deleteByMethod2(list<int> mylist)
    {
      
        // printing all the elements of the list
        cout << "\nList originally: ";
        printList(mylist);
      
        // Get the iterator
        list<int>::iterator it;
      
        // Get the positionOfElementToBeDeleted
        // using method 2
        it = --mylist.end();
      
        // Erase the last element
        // currently pointed by the iterator
        mylist.erase(it);
      
        // printing all the elements of the list
        cout << "List after deletion: ";
        printList(mylist);
    }
      
    // Driver code
    int main()
    {
        list<int> mylist;
      
        // Get the list
        for (int i = 1; i < 10; i++)
            mylist.push_back(i * 10);
      
        // Method 2 to get positionOfElementToBeDeleted
        deleteByMethod2(mylist);
      
        return 0;
    }

    Output:

    List originally:  10 20 30 40 50 60 70 80 90
    List after deletion:  10 20 30 40 50 60 70 80
    

    My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!