In this article we will see how to erase elements from a std::list using iterators.

std::list provides 2 member function to delete elements using iterators i.e.

It deletes the element representing by passed iterator “position” and returns the iterator of element next to last deleted element.

It accepts a range of elements as an argument and deletes all the elements in range (firs, last] and returns the iterator of element next to last deleted element.

Output

 

To compile above code on Linux use following command,

g++ –std=c++11 example.cpp

 

 

In the next articles we will see how to erase elements using a criteria and also in a loop.

Thanks

Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.