samedi 7 septembre 2019

Vector.end() unexpected behavior after deleting an element in C++

I'm facing a problem when I delete duplicate values in a vector. I know that when a vector value gets deleted the vector gets re-allocated for maintaining the data without any "dark holes". The problem is that when I'm using remove() inside a loop, when the code runs back to the condition statement of it, I get an assertion failed message.

my question is: if I'm using nums.end() at a condition for the iteration, does it get a static value of an iterator, or for each iteration it returns a new iterator that points to the end of the new re-allocated vector? if it does or if not, what seems to be wrong with the incrementation?

while ( itfast != nums.end()) {
        if (*itslow == *itfast) {
            nums.erase(itfast);
        }
        else {
            itslow++;
            itfast++;
        }
    }

Aucun commentaire:

Enregistrer un commentaire