Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Types of iterator : Output vs. Input vs. Forward vs. Random Access Iterator

Tags:

c++

iterator

stl

How many types of iterators are there in C++ STL? As of now, I know of these:

  • Output Iterator
  • Input Iterator
  • Forward Iterator
  • Random Access Iterator

Are there more? What are the differences between them? What are the limitations and characteristics of each? Which type is used when?

like image 882
Nawaz Avatar asked Mar 06 '11 16:03

Nawaz


People also ask

How many types of iterator are there?

There are three main kinds of input iterators: ordinary pointers, container iterators, and input streams iterators.

What is an iterator What are types of iterators?

Iterators are used to traverse from one element to another element, a process is known as iterating through the container. The main advantage of an iterator is to provide a common interface for all the containers type. Iterators make the algorithm independent of the type of the container used.

What is the difference between a bidirectional iterator and a random-access iterator?

The distinction is simply that the Bidirectional Iterator implementations are linear time, while Random Access Iterators are required to support random access to elements in amortized constant time. This has major implications for the sorts of algorithms that can sensibly be written using the two types of iterators.


3 Answers

If you can, find and read "The C++ Standard Library: A Tutorial and Reference". This book contains a whole chapter about STL iterators.

Here is a little something from the book:

Iterator Category  Ability                          Providers
-----------------  -------------------------------  ----------------------------
Input iterator     Reads forward                    istream
Output iterator    Writes forward                   ostream, inserter
Forward iterator   Reads/writes forward             forward_list,
                                                      unordered_[multi]set,
                                                      unordered_[multi]map
Bidirectional it.  Reads/writes forward/backward    list, [multi]set, [multi]map
Random access it.  Reads/writes with random access  vector, deque string, array 
like image 80
zkunov Avatar answered Oct 07 '22 11:10

zkunov


The C++ standard also has a Bidirectional Iterator concept, which is a Forward Iterator that can also go backward (with operator--). Together, these five form the entire iterator hierarchy in paragraph 24.2 of the C++ standard.

The old STL also had the concept of a Trivial Iterator. See its Iterator overview for details regarding the various iterators.

Boost designers Abrahams, Siek and Witt have presented a much more fine-grained set of iterator concepts.

like image 33
Fred Foo Avatar answered Oct 07 '22 10:10

Fred Foo


I suspect you know the answer pretty well, but anyway, these charts are very helpful in sorting this out

like image 37
davka Avatar answered Oct 07 '22 11:10

davka