C++

List Erase C++

A list is a sort of data structure that enables us to insert and delete the items from anywhere in the sequence. A list has many features like push_front(), push_back(), erase(), etc. We will apply some of them in this guide. This article will cover the erase() function of the list implemented in the C++ programming language in the Ubuntu operating system.

List erase()

It is a built-in function in C++, and it is declared in the library portion as a header file; using this function, we remove items from the list. It can be a single item or maybe a range of items. When we remove any item, the size of the dynamically created list is decreased according to the elements present.

Syntax

# iterator listname.erase(iterator position)
# iterator listname.erase(iterator first, iterator last)

Parameters

  • Position: We use this parameter to eliminate a single item from the list. This parameter contains an iterator that points towards the item to be removed from the list.
  • First/last: These arguments are used to remove a group of elements from the list. The first refers to the first item of the iterator in a range, and the last shows the last item in the range pointed by the iterator.

Example 1: Erasing a single item

This example contains a simple source code for removing a single item from the list. We know that we can remove the item in the list from any place, so we will add elements from the backside and erase the items from the front. So first, the list will be created by using an integer data type. Then we use the push_back function to enter values with the help of the list object.

List.push_back(10);

We will take print of all the elements inside the list before we eliminate any item from the list. For this purpose, a FOR loop is used. The loop will use the begin() function to start and then iterate till the end.

After that, now it’s time to create an iterator that will point towards the first position in the list to help us remove the first item. When the iterator points towards the first position, we will remove the first item from the list by using an erase() function.

List.erase(itr)

This will take the iterator object to point towards the item to be removed. After the deletion of an item, we will again take the print by using the same methodology as described above in the example.

Save the source code file with the extension of ā€˜cā€™ and then compile the code to execute it in the Ubuntu terminal. We have used a G++ compiler to compile the C++ code.

$ g++ -o file file.c
$ ./file

The resultant values show the values present in the list initialing at the time of creation. But when a single item from the front is removed, all items are displayed again.

Erasing a specific range of elements

As we have removed the first element, we will erase a range of elements from the list. To accomplish this task, we will use the same approach; the list will be created, and values will be entered into the list. And then, before the removal of data from the list, all the items will be displayed once before erasing the range of items.

Two iterators are used. The first one will point towards the first item, and the second iterator will identify the second or other items in the list.

After creation now, the second iterator will be incremented by three positions. For this operation, we have used an advance function.

Advance(itr2, 3);

The advance() function will increment the position of the second iterator. And hence by doing this, a range of items will be created. For instance, here, the second iterator will jump 3 elements, so from 0 to 3 indexes, all the values will be deleted. After deletion, now the rest of the items are displayed.

Example 2

This example will use the erase function and remove only those elements from the list multiples of number 3. All those elements that are not multiple of 3 will remain in the list. First, we will use the list library in the program to apply all the functions of the list in the program.

#include <list>

In the main program, first, create a list and then initialize random 10 numbers to the list. All the items are of integer data type. To iterate in the list among the items, we will create an iterator, and this iterator will use the begin() function to start from the front side of the list. Here we will use a while loop to provide the logic of loop iteration. Until the iterator iterates to the end of the list, keep removing elements while iterating those elements that are divisible by 3.

Otherwise, in the other part, the iterator iterates without removal when the number is not divisible by three. The remaining content will be displayed by using a for-each loop with the begin() and end() functions.

Now you can compare the resultant values with those entered initially in the main program. Only those numbers are left behind that are not divisible by 3.

Example 3

In this example, we will use the erase function with the help of iterator numbers. The number of times the iterator iterates in the list will erase that particular number from the list. Use the relevant libraries. First, we use a function that will print all the elements of the list and then print ‘NULL’ at the end of the list items. A FOR loop will iterate using an iterator with the help of begin() and end() functions.

Inside the main program, a new list is created; it is initialized by the numbers starting from 1 and lasting to 5. All the elements are entered from the backside of the list. Then we will call the function to display all the items on the list before removal.

Now create an iterator and increment it 3 times. Doing this will jump to the 4th item, as it is already present on the first item. Then use the erase() function. By doing this, the 4th item will be removed. Now again, call the function to take the result. You will see on execution that the 4th item is not present.

Now again, create two iterators. Increment the second iterator, and it will jump to the 3rd position. The erase function this time takes itr1 and itr2 both in the parameter.

So that both the initial numbers can be removed; after that, print the remaining items of the list.

Now you can see the resultant values from the terminal.

Conclusion

‘Erase list C++’ contains the description regarding the usage of erasing () in the list in the C++ programming language. This erase function removes either a single element or a bunch of elements describing the range, including the starting and ending point. We have added 4 examples that demonstrate the working of erasing functions in detail.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.