I followed the following post that illustrates the scenario how iterator behaves after some non-const operations.
I have problems to understand the difference between reference
and iterator
. Here is one of the rule listed as an example for clarification:
deque: all iterators and references are invalidated, unless the inserted member is at an end (front or back) of the deque (in which case all iterators are invalidated, but references to elements are unaffected) [23.2.1.3/1]
Here is the sample code that is based on the reference.
std::deque<int> mydeque;
mydeque.push_back(1);
mydeque.push_back(2);
mydeque.push_back(3);
const int& int3 = mydeque.back(); // reference to 3
int& int3 = mydeque.back();
std::deque<int>::iterator itBack = mydeque.crbegin(); // pointing to 3
mydeque.push_back(4);
Question> If my vague understanding is correct, then the following statement is true:
After the calling of the line of `mydeque.push_back(4)`
`int3` is still valid because it is a reference to element.
`itBack` is invalidated because it is an iterator.
Thank you