Iterators in C++

What are iterators useful for?

Iterators let you traverse the nodes of a linked list.

Can be categorized in 5 categories:
  1. Input : Sequential single pass input
  2. Output: Sequential single pass output
  3. Bidirectional:  Can go in both directions backward and forward in a list.
  4. Forward: Can go through a list from start to finish only forward direction.
  5. Random Access: Allows you to use a list like a pointer and access any point in the list. Can also be considered valid bidirectional iterators.
When your declaring a iterator you can use the following header to know what arguments to pass into the template.

template <class Category, class T, class Distance = ptrdiff_t, 
          class Pointer = T*, class Reference = T&>            
  struct iterator {                                            
    typedef T         value_type;                              
    typedef Distance  difference_type;                         
    typedef Pointer   pointer;                                 
    typedef Reference reference;                               
    typedef Category  iterator_category;                       


Popular Posts