For most containers, the iterator type provides read-write access to values in the container, and the const_iterator type provides read-only access. However, for std::set<T>, the iterator type cannot provide read-write access, because modifying a value in the set (potentially) breaks the container invariants. Therefore, in std::set<T>, both iterator and const_iterator provide read-only access.
This leads me to my question: Is there any difference between the things you can do with a std::set<T>::iterator and the things you can do with a std::set<T>::const_iterator?
Note that in C++11, the manipulation methods of containers (e.g., erase) can take const_iterator arguments.
No, there’s not much much functional difference between them. Of course, there used to be back in C++03, when
set<T>::iteratordidn’t return aconst T&. But once they changed it, they were stuck with two different kinds of iterators that both do the same thing.Indeed, the standard is quite clear that they have identical functionality (to the point where they can be the same type, but aren’t required to be). From 23.2.4, p. 6: