This effectively reduces the list size by the number of elements removed, calling each element's destructor before.
lists are sequence containers specifically designed to be efficient inserting and removing elements in any position, even in the middle of the sequence. Compared to the other base sequence containers (vector and deque), lists are the most efficient container erasing at some position other than the beginning or the end of the sequence, and, unlike in these, all of the previously obtained iterators and references remain valid after the erasing operation and refer to the same elements they were referring before (except, naturally, for those referring to erased elements).
Parameters
All parameters are of member type iterator, which in list containers are defined as a bidirectional iterator type.- position
- Iterator pointing to a single element to be removed from the list.
- first, last
- Iterators specifying a range within the list container to be removed: [first,last). i.e., the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.
Return value
A bidirectional iterator pointing to the new location of the element that followed the last element erased by the function call, which is the list end if the operation erased the last element in the sequence.Example
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
Output:
mylist contains: 10 30 60 80 90 |
Complexity
Linear on the number of elements erased (destructors).See also
| list::pop_back | Delete last element (public member function) |
| list::pop_front | Delete first element (public member function) |
| list::remove | Remove elements with specific value (public member function) |
| list::unique | Remove duplicate values (member function) |
| list::insert | Insert elements (public member function) |
