Removing Element using Single Iterator

Iterators are the components of STL used for iteration over a container. So, we can use an iterator to iterate over the list, and while traversing we can remove the elements we want to remove. Removing elements with respect to value or index can be done using this method.

Syntax:

iterator = list_name.erase(iterator);

That will update the iterator to point to the location after the iterator you removed from the list.

Remember to not do it++ in the loop because you already increment the iterator by 1 after each iteration. If you do so, then you have to do it– in each iteration of the loop for maintaining continuity.

Time Complexity: O(N)
Auxiliary Space: O(1)

Below is the implementation of the above approach.

C++




// C++ Program to implement Removing
// element from the list while iteration
#include <iostream>
#include <list>
using namespace std;
  
// Function to print elements From
// the list While iterating
void print(list<int>& li)
{
    auto it = li.begin();
    if (it == li.end()) {
        cout << "List is Empty" << endl;
    }
  
    for (it; it != li.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
  
// Function to delete elements From
// the list While iterating
  
void solve(list<int>& li)
{
    list<int>::iterator it = li.begin();
  
    // Before Deletion
    cout << "Before Deletion: " << endl;
    cout << "Size of List: " << li.size() << endl;
    print(li);
  
    // Deleting element from list while iterating
    while (it != li.end()) {
        if (*it % 3 == 0) {
            it = li.erase(it);
            continue;
        }
        it++;
    }
  
    // After Deletion
    cout << "\nAfter Deletion: " << endl;
    cout << "Size of List: " << li.size() << endl;
    print(li);
}
  
// Driver Code
int main()
{
    list<int> li = { 1, 2, 3, 5, 4, 6 };
  
    solve(li);
  
    return 0;
}


Output

Before Deletion: 
Size of List: 6
1 2 3 5 4 6 

After Deletion: 
Size of List: 4
1 2 5 4 

C++ Remove Elements From a List While Iterating

Prerequisite: List in C++

Removing elements is a necessary task for any container. Removing elements from a list is an easy task as we have predefined pop_back and pop_front functions but as the list is a container following the properties of a doubly linked list there should be a method to remove elements while iterating. 

You cannot directly delete the element from the list while iterating because of doing you get Runtime Error because the iterator is now pointing to NULL and the address of the next element of the list gets lost and you cannot access the list anymore.

Similar Reads

Removing Element using Single Iterator

Iterators are the components of STL used for iteration over a container. So, we can use an iterator to iterate over the list, and while traversing we can remove the elements we want to remove. Removing elements with respect to value or index can be done using this method....

Iterating in for loop:

...

Removing Element using two iterators

There is no such change in the while and for loop, just the way to increment is changed which can be quite confusing....

Contact Us