[toc]
1. Remove List Items Through std::clear()
The clear() function of the C++ standard list removes all the elements stored in it and it leaves the list empty. This will make sure the destructor gets called when the list stores C++ objects. Now we will look at the below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#include <stdio.h> #include <conio.h> #include <list> using namespace std; //Example 01: Clear void main() { //1.0 Create list and iterator list<int> theList; list<int>::iterator listItr; //2.0 Let us push some elements to the List theList.push_back(2); theList.push_back(4); theList.push_back(6); theList.push_back(8); //3.0 Iterate the List and Show Elements in it printf("Total Elements in the List: %d\n", theList.size()); printf("The Content of List:\n"); for (listItr = theList.begin(); listItr != theList.end(); listItr++) printf("[%d] ", *listItr); //4.0 Clear the List printf("\nClearing the List"); theList.clear(); printf("\nTotal Elements in the List: %d\n", theList.size()); //5.0 Let put two three elements printf("Adding three new int values\n"); theList.push_back(12); theList.push_back(14); theList.push_back(16); printf("Total Elements in the List: %d\n", theList.size()); printf("The Content of List:\n"); for (listItr = theList.begin(); listItr != theList.end(); listItr++) printf("[%d] ", *listItr); _getch(); } |
In the Above Example, we create a C++ list with four items 2,4,6,8. In code snippet 3.0, the C++ std::list::size() function tells us how many items exists in the list. Also, we traverse the list through iterator to print all its elements. You can read about the iterators here: Iterating the C++ List
Code snippet 4.0 makes a call to the C++ std::clear() method. Once this method is called, all the elements in the lists are removed. In our case, the clear function will remove the list elements 2,4,6,8 from the list.
Code Snippet 5.0 tells that calling clear destroys all the elements stored in the list. However, the clear function will not destroy the list. Here, we add three new elements to the same list. After that we iterate the list to display these items in the console window.
The output of the above example is below:

2. The list::Empty Function
The empty() function of the standard list checks whether list is empty or not. It will not clear the items from the list. Have a look at the below example:

In the above example, four integer elements 2,4,6,8 are added to the list. At this stage, the list is having four elements. The code marked as one, tests the list to see it is empty or not. The empty() function returns true when the list is empty. Here, in the example (Snippet, Marked as 1), it returns false as the list in not empty.
The code snippet 2 shows that the list is cleared by calling the std::list::clear() and all four integers removed from the list. Calling empty() function at this stage returns true to the caller as the list does not have any integer number(s) in it. The complete code example for list::Empty is given below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include <stdio.h> #include <conio.h> #include <list> using namespace std; //Example 01: Clear void main() { //1.0 Create list and iterator list<int> theList; list<int>::iterator listItr; //2.0 Let us push some elements to the List theList.push_back(2); theList.push_back(4); theList.push_back(6); theList.push_back(8); //3.0 Iterate the List and Show Elements in it if (theList.empty()) printf("The List is Empty\n"); else printf("The List has element(s) in it\n"); printf("Total Elements %d\n", theList.size()); //4.0 Clear and test the list printf("Clearing the Elements\n"); theList.clear(); if (theList.empty()) printf("The List is Empty\n"); else printf("The List has element(s) in it\n"); printf("Total Elements %d", theList.size()); _getch(); } |
The output of the above example is below:

3. Removing Elements through std::list::erase
3.1 Removing Single Element
C++ standard list::erase function can delete a single element or range of elements from the list. First, let us look at the example that removes single element from the list. Now we will look at the code snippet below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//1.0 Create list and iterator list<int> theList; list<int>::iterator listItr; //2.0 Let us push some elements to the List theList.push_back(2); theList.push_back(4); theList.push_back(6); theList.push_back(8); theList.push_back(10); theList.push_back(12); theList.push_back(14); printf("Current List Elelements are: "); for (listItr = theList.begin(); listItr != theList.end(); listItr++) printf("[%d] ", *listItr); |
In the above code, we added 7 integer elements to the list. After that we displayed the list elements in the list on console output window. Now, look at the below code:
1 2 3 4 |
//3.0 Remove a single Element from the List listItr = theList.begin(); listItr++; listItr++; |
We got a list iterator pointing to the first element in the list by calling the function begin() . Then incremented the list iterator twice. Now, the iterator is pointing to the third element in the list. The below picture shows the position of the list iterator:

At this stage if we call the erase function by passing the iterator as a parameter, C++ will remove the element pointed by it from the list. In our Example, the third list element (Value 6) gets removed after calling the std::list::erase(<list Iterator>) function. Below is the code snippet:
1 2 3 4 5 |
theList.erase(listItr); printf("\nList Elelements After Erasing Single Element: "); for (listItr = theList.begin(); listItr != theList.end(); listItr++) printf("[%d] ", *listItr); |
3.2 Erasing Range of elements from the List
To remove a range of list elements, we need two iterators. One iterator tells where the removal should start and another one tells where it must end. Now we will look at the below piece of code:
1 2 3 4 5 |
list<int>::iterator listItrBack = theList.begin(); list<int>::iterator listItrFront = theList.begin(); listItrBack++; listItrBack++; listItrFront = listItrBack; listItrBack++; listItrBack++; listItrBack++; |
At this stage, listItrFront iterator points to the third element in the List. Besides, listItrBack points to the last element in the list. For Example, if we read the values through the iterator now, listItrFront gives 8 and listItrBack gives 14. This is illustrated in the below picture. When we call list::erase by passing these two iterators as parameters, the items highlighted in Red gets removed from the list.

Now look at the below piece of code. Here, we are calling std::list::Erase by passing two iterators to it. After the call, C++ will remove the elements highlighted in the picture above. The code snippet is below:
1 2 3 4 5 6 7 8 9 10 11 |
//4.0 Remove Range of Elements list<int>::iterator listItrBack = theList.begin(); list<int>::iterator listItrFront = theList.begin(); listItrBack++; listItrBack++; listItrFront = listItrBack; listItrBack++; listItrBack++; listItrBack++; theList.erase(listItrFront, listItrBack); printf("\nList Elelements After Erasing Range of Elements: "); for (listItr = theList.begin(); listItr != theList.end(); listItr++) printf("[%d] ", *listItr); |
The complete example and its output is below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#include <stdio.h> #include <conio.h> #include <list> using namespace std; //Example 01: Clear void main() { //1.0 Create list and iterator list<int> theList; list<int>::iterator listItr; //2.0 Let us push some elements to the List theList.push_back(2); theList.push_back(4); theList.push_back(6); theList.push_back(8); theList.push_back(10); theList.push_back(12); theList.push_back(14); printf("Current List Elelements are: "); for (listItr = theList.begin(); listItr != theList.end(); listItr++) printf("[%d] ", *listItr); //3.0 Remove a single Element from the List listItr = theList.begin(); listItr++; listItr++; theList.erase(listItr); printf("\nList Elelements After Erasing Single Element: "); for (listItr = theList.begin(); listItr != theList.end(); listItr++) printf("[%d] ", *listItr); //4.0 Remove Range of Elements list<int>::iterator listItrBack = theList.begin(); list<int>::iterator listItrFront = theList.begin(); listItrBack++; listItrBack++; listItrFront = listItrBack; listItrBack++; listItrBack++; listItrBack++; theList.erase(listItrFront, listItrBack); printf("\nList Elelements After Erasing Range of Elements: "); for (listItr = theList.begin(); listItr != theList.end(); listItr++) printf("[%d] ", *listItr); _getch(); } |
