ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

C++ std::list clear vs erase Explained with Examples

Updated on December 12, 2017
sirama profile image

I am a software engineer. I have been come across C++,MFC, .net technologies. I like playing video games & reading books.

1. Clearing all the std::List Elements through std::Clear()

The clear() function of the 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 or simply it destroys all the objects. Let us have a look at the below Code:

#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, a list is created and four elements namely 2,4,6,8 is pushed into it. In Code snippet 3.0, the "std::size()" method called on the list tells how many elements present in the list and also here we traverse the list through iterator to list all the elements.

Code snippet 4.0, makes a call to the "std::clear()" method. Once this method is called, all the elements in the lists are removed. In our case, the list elements 2,4,6,8 will be removed from the list.

Code Snippet 5.0 tells that calling clear destroys all the elements stored in the list and at the same time, the list still lives. Here, the same list is added with three new elements and then the elements are iterated to display its value in the console window.

The output of the above example shown below:

Pic1. Program output of Std::List::Clear Example
Pic1. Program output of Std::List::Clear Example | Source

2. The list::Empty

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

list::Empty Example
list::Empty Example | Source

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:

#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 shown below:


Output of std::List::Empty Example
Output of std::List::Empty Example | Source

3. Removing Elements through std::list::erase

3.1 Removing Single Element

The Erase function is used to delete a single element or range of elements from the list. First, let us look at the example that erases single element from the list. Consider the code snippet below:


//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:

//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 and now, the iterator is pointing to the third element in the list. This shown in the below picture:


Pic 1. list::erase - Single Element
Pic 1. list::erase - Single Element | Source

At this stage if we call the erase function by passing the iterator as parameter, the element pointed by it will be removed 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:

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 range of continuous list elements we need to iterators. One iterator tells where the removal should start and another one tells where it ends. Have a look at the below piece of code:

list<int>::iterator listItrBack = theList.begin();
list<int>::iterator listItrFront = theList.begin();
listItrBack++; listItrBack++;
listItrFront = listItrBack;
listItrBack++; listItrBack++; listItrBack++;

At this stage, listItrFront points to the third element in the List and listItrBack points to the last element in the list. For Example, if read the values through the iterator values 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.

Pic: Erasing range of element from C++ List
Pic: Erasing range of element from C++ List | Source

Now look at the below piece of code. Here, we are calling std::list::Erase by passing the two iterators to it. After the call, the elements highlighted in the picture above will get removed. The code snippet is shown below:

//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 given below:

#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();
}
Pic: Erase Range of C++ List Elements - Program Output
Pic: Erase Range of C++ List Elements - Program Output | Source

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working