简体   繁体   中英

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

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM