Does removal to a deque object invalidate its iterators?

I read from my textbook that all iterators of a deque object should be invalidated if an element is removed from places that not the front or back of the deque object, so I wrote the code below to test this statement.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <deque>
using namespace std;
int main()
{
    deque<int> a = {1,2,3,4,5,6,7,8};
    auto it = a.begin() + 5; //iterator corresponding to 6 in 'a'.
    a.erase(a.begin() + 1);
    cout << (it == a.begin() + 4); //here 'a.begin() + 4' also corresponds to 6 in 'a' after 2 being removed.
}

To my surprise, the iterator that I stored in the object 'it' corresponding to the element 6 before the removal of the element 2 equals to the the iterator that corresponds to element 6 after the removal, which implies that the iterators are not invalidated. Am I wrong or the statement from the textbook is wrong?
The iterators are invalidated.
There is no guarantee that (it == a.begin() + 4) would be well defined, and that it would evaluate to true.

Erase:
An erase operation that erases the last element of a deque invalidates only the past-the-end iterator and all iterators and references to the erased elements. An erase operation that erases the first element of a deque but not the last element invalidates only the erased elements. An erase operation that erases neither the first element nor the last element of a deque invalidates the past-the-end iterator and all iterators and references to all the elements of the deque.
Insert:
An insertion in the middle of the deque invalidates all the iterators and references to elements of the deque. An insertion at either end of the deque invalidates all the iterators to the deque, but has no effect on the validity of references to elements of the deque.
Thanks for the clarification. I appreciate it!
Topic archived. No new replies allowed.