I just wanted to ask if iterators are valid after sorting or removal of elements. When we insert into a container, it can be reallocated to find more space and invalidate existing iterators. But what happens with sorting and removal of elements? Can those force the container to be reallocated?
Is this code going to crash once?
QVector<Foo> container;
b = container.begin();
e = container.end();
while (container.count() > newCount)
{
computeSomePropertyOfFoo(b, e); // Computes property between b and e
sortByThatProperty(b, e); // sorts members between b and e
container.erase(--e);
}
From the Container Classes documentation:
So yes, your code is incorrect. You shouldn’t "cache" the iterators the way you do it.