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, const_iterator endingPositionIterator_inclusive);
Below is the implementation of the above approach:
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
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- How to delete a range of values from the Set using Iterator
- Queries for elements having values within the range A to B in the given index range using Segment Tree
- Implementing Iterator pattern of a single Linked List
- Delete elements in C++ STL list
- How to delete last element from a List in C++ STL
- <iterator> library in C++ STL
- Iterator Invalidation in C++
- Implementing Forward Iterator in BST
- C++ program to find the type of the given iterator
- Cyclic Iterator for K variable length vectors
- All possible values of floor(N/K) for all values of K
- "delete this" in C++
- delete() in C++
- delete and free() in C++
- How to delete last element from a set in C++
- How to delete last element from a map in C++
- new vs malloc() and free() vs delete in C++
- Overloading New and Delete operator in c++
- How to delete an element from the Set by passing its value in C++
- Delete middle element of a stack
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.