How to delete last element from a List in C++ STL
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:
- 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>usingnamespacestd;// Function to print the listvoidprintList(list<int> mylist){// Get the iteratorlist<int>::iterator it;// printing all the elements of the listfor(it = mylist.begin(); it != mylist.end(); ++it)cout <<' '<< *it;cout <<'\n';}// Function to delete last element of list// using method 1voiddeleteByMethod1(list<int> mylist){// printing all the elements of the listcout <<"\nList originally: ";printList(mylist);// Get the iteratorlist<int>::iterator it;// Get the positionOfElementToBeDeleted// using method 1it = prev(mylist.end());// Erase the last element// currently pointed by the iteratormylist.erase(it);// printing all the elements of the listcout <<"List after deletion: ";printList(mylist);}// Driver codeintmain(){list<int> mylist;// Get the listfor(inti = 1; i < 10; i++)mylist.push_back(i * 10);// Method 1 to get positionOfElementToBeDeleteddeleteByMethod1(mylist);return0;}chevron_rightfilter_noneOutput:List originally: 10 20 30 40 50 60 70 80 90 List after deletion: 10 20 30 40 50 60 70 80
- 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>usingnamespacestd;// Function to print the listvoidprintList(list<int> mylist){// Get the iteratorlist<int>::iterator it;// printing all the elements of the listfor(it = mylist.begin(); it != mylist.end(); ++it)cout <<' '<< *it;cout <<'\n';}// Function to delete last element of list// using method 2voiddeleteByMethod2(list<int> mylist){// printing all the elements of the listcout <<"\nList originally: ";printList(mylist);// Get the iteratorlist<int>::iterator it;// Get the positionOfElementToBeDeleted// using method 2it = --mylist.end();// Erase the last element// currently pointed by the iteratormylist.erase(it);// printing all the elements of the listcout <<"List after deletion: ";printList(mylist);}// Driver codeintmain(){list<int> mylist;// Get the listfor(inti = 1; i < 10; i++)mylist.push_back(i * 10);// Method 2 to get positionOfElementToBeDeleteddeleteByMethod2(mylist);return0;}chevron_rightfilter_noneOutput:List originally: 10 20 30 40 50 60 70 80 90 List after deletion: 10 20 30 40 50 60 70 80
Recommended Posts:
- How to delete last element from a map in C++
- How to delete last element from a set in C++
- How to delete an element from the Set by passing its value in C++
- Delete middle element of a stack
- Delete elements in C++ STL list
- How to delete a range of values from the List using Iterator
- Queries to insert, delete one occurrence of a number and print the least and most frequent element
- delete() in C++
- "delete this" in C++
- delete and free() in C++
- Overloading New and Delete operator in c++
- new and delete operators in C++ for dynamic memory
- How to delete a range of values from the Set using Iterator
- Different ways to delete elements in std::map (erase() and clear())
- Minimum delete operations to make all elements of array same
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.



