More about Iterators
As we have already known, iterators are useful to traverse elements in a STL container. However, there are also other types of iterators that are commonly used.
iterator
iterator is the normal type of iterators we have learned before. It supports forward traversal of containers. We can use the deference to access the element it points to, and modify it as well.
const_iterator
const_iterator supports forward traversal of containers. We can use the deference to access the element it points to, but cannot modify it, since the deference returns a const
reference to the element.
reverse_iterator
reverse_iterator supports backward traversal of containers. We can use the deference to access the element it points to, and modify it as well.
Instead of begin() and end() in normal iterators, we use rbegin() and rend() in reverse iterators. rbegin() returns a reverse_iterator to the last element in the container, while rend() returns a reverse_iterator to the one before the first element in the container.
const_reverse_iterator
reverse_iterator supports backward traversal of containers. We can use the deference to access the element it points to, but cannot modify it, since the deference returns a const
reference to the element.
Last updated