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:

STL Iterators

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.

Similar Reads

What is Standard Template Library(STL)?

The C++ Standard Template Library (STL) is a collection of generic class and function templates to provide some commonly used data structures and algorithms. It contains optimized and error-free code for useful containers such as vector, list, stack, queue, etc. It is a part of the standard library of C++ and...

Components of STL

C++ STL provides various components to make programming easier and more efficient. These components can be divided into four categories:...

STL Containers

The STL containers are the template classes to implement useful data structures such as dynamic arrays, hashmaps, linked lists, trees, etc. These containers allow programmers to store and manipulate data....

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

STL Algorithms

Algorithms are set of generic and optimal implementations of some useful algorithms to make programming easier and more efficient....

STL Function Objects (Functors)

The Function Objects, also known as Functors, are the objects that behave like a function. It is due to the overloading of the ( ) parenthesis operator. The functors are defined inside the header file....

Contact Us