See related questions on past-the-end iterator invalidation: this, this.
This is more a question of design, namely, is there (in STL or elsewhere) such concept as past-the-end iterator "revalidation"?
What I mean by this, and use case: suppose an algorithm needs to "tail" a container (such as a queue). It traverses the container until end()
is reached, then pauses; independently from this, another part of the program enqueues more items in the queue. How is it possible for the algorithm to (EDIT) efficiently tell, "have more items been enqueued" while holding the previously past-the-end iterator (call it tailIt
)? (this would imply it is able to check if tailIt == container.end()
still, and if that is false, conclude tailIt
is now valid and points to the first element that was inserted).
Please don't dismiss the question as "no, there isn't" - I'm looking to form judgment around how to design some logic in an idiomatic way, and have many options (in fact the iterators in question are to a hand-built data structure for which I can provide this property - end() revalidation - but I would like to judge if it is a good idea).
EDIT: made it clear we have the iterator tailIt
and a reference to container
. A trivial workaround for what I'm trying to do is, also remember count
:= how many items you processed, and then check is container.size() == count
still, and if not, seek to container[count]
and continue processing from there. This comes with many disadvantages (extra state, assumption container doesn't pop from the front (!), random-access for efficient seeking).
std::vector<T>
and an iteratorT* p
in the form of a raw pointer that points at the end (= end()
). Then you expand the vector without reallocation. Would you be able to determine if the vector has been expanded if you only have the value ofp
? – Evgc
, so you could ask isp == c.end()
still? For the vector case and only if there was no reallocation, p would point to the first newly inserted element. Editing the question. – haelix