# 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.

```cpp
int main() {
    vector<int> v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
    vector<int>::iterator it;
    for (it = v.begin(); it != v.end(); ++it) {
        *it += 1;
        cout << *it << " "; // 1 2 3 4 5 6 7 8 9 10
    }
    return 0;
}
```

### _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.

```cpp
int main() {
    vector<int> v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
    vector<int>::const_iterator it;
    for (it = v.begin(); it != v.end(); ++it) {
        *it += 1;   // ERROR
        cout << *it << " ";
    }
    return 0;
}
```

### _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.

```cpp
int main() {
    vector<int> v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
    vector<int>::reverse_iterator it;
    for (it = v.rbegin(); it != v.rend(); ++it) {
        *it += 1;
        cout << *it << " "; // 10 9 8 7 6 5 4 3 2 1
    }
    return 0;
}
```

### _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.

```cpp
int main() {
    vector<int> v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
    vector<int>::const_reverse_iterator it;
    for (it = v.rbegin(); it != v.rend(); ++it) {
        *it += 1;   // ERROR
        cout << *it << " ";
    }
    return 0;
}
```
