Checking if an iterator is valid

后端 未结 11 561
耶瑟儿~
耶瑟儿~ 2020-11-30 02:52

Is there any way to check if an iterator (whether it is from a vector, a list, a deque...) is (still) dereferencable, i.e. has not been invalidated?

I have been usi

11条回答
  •  天命终不由人
    2020-11-30 03:14

    In some of the STL containers, the current iterator becomes invalid when you erase the current value of the iterator. This happens because the erase operation changes the internal memory structure of the container and increment operator on existing iterator points to an undefined locations.

    When you do the following, iterator is incementented before it is passed to erase function.

    if (something) l.erase(itd++);

提交回复
热议问题