C++ std::list Clear vs Erase vs Empty Explained


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:

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:

std list clear program output

std list clear program output

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:

std list empty c++ code example

std list empty c++ code 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:

The output of the above example is below:

Code output

std list empty c++ code example output

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:

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:

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:

std::list::erase - Remove Single Element

std::list::erase – Remove Single Element

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:

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:

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.

Erasing range of element from C++ List

Erasing range of element from C++ 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:

The complete example and its output is below:

Program Output

Program Output


Categories: C++

Tags: , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: