STL Iterators
Iterators are the objects used to iterate through the STL containers. They can be seen as pointers that are used to traverse and manipulate the data inside containers.
- Iterators are defined inside the <iterator> header file.
- Each container has its own iterators.
Iterators can be classified into 5 types which are:
1. Input Iterator
Input Iterators are used for single-pass input operations.
- They can only be used for accessing (read operations) not assigning.
- They cannot be decremented.
- An element can only be accessed once.
- They have limited capability and come lowest in the iterator hierarchy.
istream_iterator is an example of the input iterator.
2. Output Iterator
Output Iterators are used for single-pass output operations.
- They can only be used for assigning purposes (write operations).
- An element can only be accessed once.
- They cannot be decremented.
- They come lowest in the hierarchy along with the Input Iterators.
ostream_iterator is an example of the output iterator.
3. Forward Iterator
Forward iterators contain features of both input and output iterators along with:
- It can be used for both read and write operations.
- It cannot be decremented as it can move only in a single direction.
- It can only move sequentially i.e., one step at a time.
- It is in the upper hierarchy compared to both input and output iterators.
forward_list::iterator are examples of the forward iterators.
4. Bi-Directional Iterator
The bi-directional iterators have all the features of forward iterators along with:
- They can move in both forward and backward directions.
- They can be used for both read and write operations.
map::iterator, set::iterator, multiset::iterator, and multimap::iterators are some examples of input iterator.
5. Random Access Iterator
Random access iterators are the most powerful iterators.
- They contain features of all the other iterators.
- They can move in both forward and backward directions.
- Read and write operations can be performed.
- Can move to any point in the container i.e. random access.
vector::iterator and array::iterator are some examples.
C++ STL Cheat Sheet
The C++ STL Cheat Sheet provides short and concise notes on Standard Template Library (STL) in C++. Designed for programmers that want to quickly go through key STL concepts, the STL cheatsheet covers the concepts such as vectors and other containers, iterators, functors, etc., with their syntax and example.