C++ List Library - erase_range() Function
Description
The C++ function std::list::erase_range() removes range of element from the the list and modifies size of list.
Declaration
Following is the declaration for std::list::erase_range() function form std::list header.
C++98
iterator erase (iterator first, iterator last);
C++11
iterator erase (const_iterator first, const_iterator last);
Parameters
first − Input iterator to the initial position in range.
last − Input iterator to the final position in range.
Return value
Returns a random access iterator.
Exceptions
If range is invalid then behavior is undefined.
Time complexity
Linear i.e. O(n)
Example
The following example shows the usage of std::list::erase_range() function.
#include <iostream>
#include <list>
using namespace std;
int main(void) {
list<int> l = {1, 2, 3, 4, 5};
cout << "Size of list befor erase operation = " << l.size() << endl;
l.erase(l.begin(), l.end());
cout << "Size of list after erase operation = " << l.size() << endl;
return 0;
}
Let us compile and run the above program, this will produce the following result −
Size of list befor erase operation = 5 Size of list after erase operation = 0
list.htm