Iterator library

From Cppreference

< cpp
Jump to: navigation, search

Iterators are a generalization of pointers that allow C++ programs to access different containers in a uniform manner. Any algorithm that accepts iterators should accept regular pointers also.

Contents

[edit] Iterator Types

Five iterator categories are defined, according to operations defined on them:

  • Input iterator
allows reading from the iterator object
allows iterating forward without multi-pass guarantee, i. e. reading the same element twice is not guaranteed to produce the same results
  • Output iterator
allows writing to the iterator object
allows iterating forward without multi-pass guarantee
  • Forward iterator
allows reading from the iterator object
allows iterating forward with multi-pass guarantee, i. e. reading the same element twice is guaranteed to produce the same results
  • Bidirectional iterator
allows reading from the iterator object
allows iterating forward and backward with multi-pass guarantee
  • Random access iterator
allows reading from the iterator object
allows iterating forward and backward randomly with multi-pass guarantee

Forward iterators satisfy the requirements of input iterators

[edit] Iterator Operations

Defined in header <iterator>
advance
advances an iterator by given distance
(function)
distance
returns the distance between two iterators
(function)
next
increment an iterator
(function)
prev
decrement an iterator
(function)

[edit] Predefined Iterators

reverse_iterator, insert_iterator, move_iterator

[edit] Stream Iterators

istream_iterator, ostream_iterator, istreambuf_iterator

[edit] Range Access

Defined in header <iterator>
begin (C++11)
returns an iterator to the beginning of a container or array
(function)
end (C++11)
returns an iterator to the end of a container or array
(function)