C++C++11C++14C++17IteratorsLearn C++

Categories of Iterators in C++

sea washing sandy seaside in resort

Conceptually, iterators are consists of five categories:

  1. input iterator is intended to traverse sequences in the forward direction and provides the read access to the pointed sequence element;
  2. output iterator is intended to traverse sequences in the forward direction and provides the write access to the pointed sequence element;
  3. forward iterator is intended to traverse sequences in the forward direction and provides both read and write access to the pointed sequence element;
  4. bidirectional iterator is intended to traverse sequences in both forward and backward directions and provides the same access to the pointed sequence element as forward iterator;
  5. random access iterator is similar to bidirectional iterator, but in addition, provides the possibility of arithmetic similar to pointer arithmetic.

The operators ++ and * are applicable to iterators of all the categories described above.

The operators ==, != and -> are applicable to iterators of all the categories described above, except the output iterators.

The operator -- is applicable to bidirectional iterators and random access iterators only.

The following possibilities are applicable only to the random access iterators:

  • it’s possible to add or subtract an integer to create a random access iterator that points to another element of the sequence;
  • it’s possible to subtract one random access iterator from another one to calculate the distance between the two elements of sequence;
  • it’s possible to subscript random access iterator by applying the operator [];
  • it’s possible to compare random access iterators by using any of the following operators: <, <=, ==, !=, >, >=.

Please note, that the calculation of the distance between two non random access iterators is usually much more expensive than subtraction of random access iterators between each other. That’s why the standard library provides the special template function called std::distance (defined in <iterator>) for this purpose instead of operator -.

Finally, please note, that prefix form of operators ++ and -- is usually preferable over the postfix form since the former returns a reference to a modified iterator whereas postfix form is obliged to return a copy of iterator object before incrementing or decrementing and hence likely to be less efficient.

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

Related posts
C++ComponentsLanguage FeatureLearn C++

How To Add Shadow Effects To Your C++ Apps

C++ComponentsLanguage FeatureLearn C++

How To Make Controls Have A Glow Effect In C++?

C++Learn C++

What Is The sscanf Function In C++ And How Can I Use It?

C++C++17Introduction to C++Language FeatureLearn C++

This Is How To Use Parallel Programming in C++ Builder

Worth reading...
Introduction to C++ Iterators
jaJapanese