Namespaces
Variants
Views
Actions

Iterator library

From cppreference.com
< cpp
Revision as of 23:28, 5 August 2013 by Serkan (Talk | contribs)

The iterator library provides definitions for five kinds of iterators as well as iterator traits, adapters, and utility functions.

Contents

Iterator categories

There are five kinds of iterators: InputIterator, OutputIterator, ForwardIterator, BidirectionalIterator, and RandomAccessIterator.

Instead of being defined by specific types, each category of iterator is defined by the operations that can be performed on it. This definition means that any type that supports the necessary operations can be used as an iterator -- for example, a pointer supports all of the operations required by RandomAccessIterator, so a pointer can be used anywhere a RandomAccessIterator is expected.

Four of the five iterator categories can be organized into a hierarchy, where more powerful iterator categories (e.g. RandomAccessIterator) support the operations of less powerful categories (e.g. InputIterator). If an iterator falls into one of these categories and also satisfies the requirements of OutputIterator, then it is called a mutable iterator and supports both input and output. Non-mutable iterators are called constant iterators.

Iterator category Defined operations
RandomAccessIterator BidirectionalIterator ForwardIterator InputIterator
  • read
  • increment (without multiple passes)
  • increment (with multiple passes)
  • decrement
  • random access

Iterators that fall into one of the above categories and also meet the requirements of OutputIterator are called mutable iterators.

OutputIterator
  • write
  • increment (without multiple passes)

Iterator primitives

provides uniform interface to the properties of an iterator
(class template) [edit]
empty class types used to indicate iterator categories
(class) [edit]
the basic iterator
(class template) [edit]

Iterator adaptors

iterator adaptor for reverse-order traversal
(class template) [edit]
iterator adaptor which dereferences to an rvalue reference
(class template) [edit]
creates a std::move_iterator of type inferred from the argument
(function template) [edit]
iterator adaptor for insertion at the end of a container
(class template) [edit]
creates a std::back_insert_iterator of type inferred from the argument
(function template) [edit]
iterator adaptor for insertion at the front of a container
(class template) [edit]
creates a std::front_insert_iterator of type inferred from the argument
(function template) [edit]
iterator adaptor for insertion into a container
(class template) [edit]
creates a std::insert_iterator of type inferred from the argument
(function template) [edit]

Stream iterators

input iterator that reads from std::basic_istream
(class template) [edit]
output iterator that writes to std::basic_ostream
(class template) [edit]
input iterator that reads from std::basic_streambuf
(class template) [edit]
output iterator that writes to std::basic_streambuf
(class template) [edit]

Iterator operations

Defined in header <iterator>
advances an iterator by given distance
(function) [edit]
returns the distance between two iterators
(function) [edit]
(C++11)
increment an iterator
(function) [edit]
(C++11)
decrement an iterator
(function) [edit]

Range access

Defined in header <iterator>
(C++11)(C++14)
returns an iterator to the beginning of a container or array
(function) [edit]
(C++11)(C++14)
returns an iterator to the end of a container or array
(function) [edit]
returns a reverse iterator to a container or array
(function) [edit]
(C++14)
returns a reverse end iterator for a container or array
(function) [edit]