Erase without invalidating iterator

As in Russia people says the law is not writtten for stupids. by the way it is interesting what standard container does define erase which returns void?

For example, in Sequence container requirements is written that erase shall return iterator.

And a qualified programmer writes code that satisfies these requirements.

So a qualified programmer will write erase function that similar to other erase functions of standard containers and returns next iterator. An unqualified programmer will write erase which returns void.

My questions is: By calling the "erase()" function, will elements from the container be "re-balanced"? Should work, the erase operator only invalidates the iterator used for erasing. containers and erase() vector - all iterators greater than and including the erased element are invalidated map/set - only the iterator pointing to the erased element is invalidated list - only the iterator to the erased element is invalidated, you can get the next element as a return values from the erase method it is very and very bad practice That shows only that the programmer is low qualified. Consider for example if your collection is of type std::vector.

Perhaps I may need to elaborate my concerns/questions a little here ... 1) Will "it Next" still be pointing the correct next element? What you will get as result of execution this code?!

So the correct code wiil look like You can write any bad code if you have so low qualification.

But the C Standard lists requirements for iterators and containers.

Storing an iterator, reference, or pointer to an element within a container for any length of time comes with a risk that the underlying container may be modified such that the stored iterator, pointer, or reference becomes invalid.

For instance, when a sequence container such as requires an underlying reallocation, outstanding iterators, pointers, and references will be invalidated [Kalev 99].

SHOW COMMENTS

Comments Erase without invalidating iterator

The Latest from altay-blog.ru ©