How to Erase Elements from a List in CPP using Iterators

Lists are sequence containers that allow for the allocation of non-contiguous memory. List traversal is slower than vector traversal, but once a position is found, insertion and deletion are fast. Normally, when we talk about a List, we mean a doubly linked list. We use a forward list to implement a singly linked list.

Given a list, the task is to erase the elements from the list using iterators in C++.

Erase Elements from a List in C++ using Iterators

1)erase() function

In C++ STL, erase() is a built-in feature for deleting items from a list container. This function can be used to delete a single element from a list container or a set of elements.

Syntax:

iterator given_list.erase(iterator position)

                                                                                      ( or )

iterator given_list.erase(iterator first, iterator last)

Parameters:

 Depending on whether this feature is used to delete a single element or 
 a set of elements from the list container, it can accept a variety of parameters.

Position:

 When the function is used to delete a single element, this parameter is used. 
This parameter is an iterator that points to the element that needs to be removed from the list container.

first, last:

 These two parameters are used when using the list to remove elements from a range. 
The parameter first refers to the iterator that points to the first element in the range, and the parameter last refers to the iterator that points to the last element in the range that needs to be erased. This deletes all of the elements in the range, including the iterator's first element but excluding the iterator's last element.

Return:

This function returns an iterator pointing to the element in the list container that came after the last element that was removed from the list container.

2)Erasing elements from list using erase()

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // Make a list and initialize it with some elements.
    list<int> arraylist(
        { 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7 });
    // take a iterator which points to first element of the
    // list
    list<int>::iterator itr = arraylist.begin();
    // print the original list
    cout << "Original list" << endl;
    for (auto itr = arraylist.begin();
         itr != arraylist.end(); ++itr)
        cout << *itr << " ";
    cout << endl;
    // suppose we want to remove second element in arraylist
    // increment the iterator to achieve this
    itr++;
    // remove this element from the list
    arraylist.erase(itr);
    // print the list after removing it
    cout
        << "printing the list after removing second element"
        << endl;
    for (auto itr = arraylist.begin();
         itr != arraylist.end(); ++itr)
        cout << *itr << " ";
    cout << endl;
    itr = arraylist.begin();
    // increment the iterator
    itr++;
    // removing a range of elements using erase
    // removing all elements from secondposition
    // to end of list
    arraylist.erase(itr, arraylist.end());
    cout << "printing the list after range of elements i.e "
            "from 2nd to end"
         << endl;
    for (auto itr = arraylist.begin();
         itr != arraylist.end(); ++itr)
        cout << *itr << " ";
}

Output:

Original list
3 4 5 6 7 8 9 1 2 3 4 5 6 7 
printing the list after removing second element
3 5 6 7 8 9 1 2 3 4 5 6 7 
printing the list after range of elements i.e from 2nd to end
3

 
Related Programs: