C++ iterators problem

前端 未结 3 1773
傲寒
傲寒 2021-01-20 15:15

I\'m working with iterators on C++ and I\'m having some trouble here. It says \"Debug Assertion Failed\" on expression (this->_Has_container()) on line interIterator++. Dist

3条回答
  •  生来不讨喜
    2021-01-20 15:56

    vector's erase() returns a new iterator to the next element. All iterators to the erased element and to elements after it become invalidated. Your loop ignores this, however, and continues to use interIterator.

    Your code should look something like this:

    if (condition)
        interIterator = externIterator->erase(interIterator);
    else
        ++interIterator;  // (generally better practice to use pre-increment)
    

提交回复
热议问题