For example is the following valid?
std::vector<int> vec(5, 0);
std::vector<int>::const_iterator it1(vec.begin());
std::vector<int>::const_iterator it2(vec.begin());
//Use it1 and it2 like they don't know about each other.
Is there a special name for a container that permits multiple activ开发者_高级运维e iterators?
Yes, it's valid.
You can have as many iterators into a vector as your system has memory to hold iterators.
The special name for this type of container is "any STL container". All containers allow this.
Maybe explain why you think this shouldn't be allowed?
As long as you don't do operations that render the other iterators invalid, there isn't any limit on the # of iterators. Some operations (like insert or remove) may render all of the other iterators invalid. The way STL container iterators work, there's no way for them to (generally) deal with insert and remove done through other iterators.
Note that using container functions to insert/remove elements will ALSO render the iterators invalid.
The underlying issue is that STL containers don't know anything about the active iterators, so they can't tell the iterators that something has changed.
That's definitely permissible. The thing you have to worry about is if you start erasing or inserting elements. Different containers have different behavior for invalidation of iterators by modifying functions, you'll have to look at the documentation. erase
on a vector
, for example, invalidates all iterators and element references after the element that was erased.
精彩评论