deque::erase | public member function |
iterator erase ( iterator position ); iterator erase ( iterator first, iterator last ); |
Erase elements
Removes from the deque container either a single element (position) or a range of elements ([first,last)).
This effectively reduces the container size by the number of elements removed, calling each element's destructor before.
Double-ended queues are designed to be efficient removing (and inserting) elements at either the end or the beginning of the sequence. Removals on other positions are usually less efficient than in list containers.
If the erasing is performed on the beginning or the end of the sequence, only the iterators and references to the erased elements are invalidated. If the deletion happens in the middle of the deque, all iterators and references are invalidated.
Parameters
All parameters are of member type iterator, which in deque containers are defined as a random access iterator type.- position
- Iterator pointing to a single element to be removed from the deque.
- first, last
- Iterators specifying a range within the deque] 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 random access iterator pointing to the new location of the element that followed the last element erased by the function call, which is the container end if the operation erased the last element in the sequence.Example
// erasing from deque #include <iostream> #include <deque> using namespace std; int main () { unsigned int i; deque<unsigned int> mydeque; // set some values (from 1 to 10) for (i=1; i<=10; i++) mydeque.push_back(i); // erase the 6th element mydeque.erase (mydeque.begin()+5); // erase the first 3 elements: mydeque.erase (mydeque.begin(),mydeque.begin()+3); cout << "mydeque contains:"; for (i=0; i<mydeque.size(); i++) cout << " " << mydeque[i]; cout << endl; return 0; } |
mydeque contains: 4 5 7 8 9 10 |
Complexity
Linear on the number of elements erased (destructors). Plus, depending on the particular library implemention, additional linear time in up to the number of elements between position and one of the ends of the deque.See also
deque::pop_back | Delete last element (public member function) |
deque::insert | Insert elements (public member function) |