std::deque : What does "insertion and deletion of elements may invalidate iterators" mean?

StackOverflow https://stackoverflow.com/questions/22285576

  •  11-06-2023
  •  | 
  •  

Domanda

I was reading about the std::deque container and the document states that

Insertion and deletion of elements in std::deque may invalidate all its iterators

Here is my version of understanding of the above statement kindly let me know if I am misinterpreting the statement or missing something

Consider the following code

std::deque<int> s;
s.push_back(12);
auto i = s.begin();
s.push_front(45);//After pushing 45 at the back now `i` may be invalid. 

Is this understanding correct ?

È stato utile?

Soluzione

You are correct. For example after

std::deque<int> s;
s.push_back(12);
auto i = s.begin();
s.push_front(45)

calling *i is undefined behavior.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top