Here's a slightly different way to look at it. Const_iterator
almost never makes sense when you are passing it as a pointer into a specific collection and you are passing the collection as well. Mr. Meyer was specifically stating that const_iterator
cannot be used with most member functions of a collection instance. In that case, you will need a plain-old iterator
. However, if you don't have a handle to the collection, the only difference between the two is that you can modify what is pointed to by an iterator
and you can't modify the object referenced by a const_iterator
.
So... you want to use iterator
whenever you are passing a collection and position into the collection to an algorithm. Basically, signatures like:
void some_operation(std::vector<int>& vec, std::vector::const_iterator pos);
don't make a whole lot of sense. The implicit statement is that some_operation
is free to modify the underlying collection but is not allowed to modify what pos
references. That doesn't make much sense. If you really want this, then pos
should be an offset instead of an iterator.
On the flip side, most of the algorithms in the STL are based on ranges specified by a pair of iterators. The collection itself is never passed so the difference between iterator
and const_iterator
is whether the value in the collection can be modified through the iterator or not. Without a reference to the collection, the separation is pretty clear.
Hopefully that made things as clear as mud ;)