How to delete a range of values from the List using Iterator
Given a List, the task is to delete a range of values from this List using Iterator.
Input: list = [10 20 30 40 50 60 70 80 90],
start_iterator = 3,
end_iterator = 8
Output: 10 20 80 90
Input: list = [1 2 3 4 5]
start_iterator = 1,
end_iterator = 3
Output: 3 4 5
Approach: In this method, a range of elements are deleted from the list. This is done with the help of two iterators. The first iterator points at the starting element of the range and the second iterator points at the last element of the range. The first iterator is exclusive while the last iterator is inclusive which means that element will also get deleted which is pointed by the last iterator.
iterator erase (const_iterator startPositionIterator_exclusive,
Below is the implementation of the above approach:
(it = mylist.begin(); it != mylist.end(); ++it)
"\nList originally: "
>::iterator start_itr = mylist.begin();
>::iterator end_itr = mylist.end();
"List after deletion of range"
" from 3rd till 2nd last: "
i = 1; i < 10; i++)
mylist.push_back(i * 10);
List originally: 10 20 30 40 50 60 70 80 90
List after deletion of range from 3rd till 2nd last: 10 20 80 90