Namespaces
Variants
Views
Actions

Iterator library

From cppreference.com
< cpp
Revision as of 18:41, 7 November 2012 by Nate (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

Template:cpp/iterator/dcl list iterator traitsTemplate:cpp/iterator/dcl list iterator tagsTemplate:cpp/iterator/dcl list iterator

Iterator adaptors

Template:cpp/iterator/dcl list reverse iteratorTemplate:cpp/iterator/dcl list move iteratorTemplate:cpp/iterator/dcl list make move iteratorTemplate:cpp/iterator/dcl list back insert iteratorTemplate:cpp/iterator/dcl list back inserterTemplate:cpp/iterator/dcl list front insert iteratorTemplate:cpp/iterator/dcl list front inserterTemplate:cpp/iterator/dcl list insert iteratorTemplate:cpp/iterator/dcl list inserter

Stream iterators

Template:cpp/iterator/dcl list istream iteratorTemplate:cpp/iterator/dcl list ostream iteratorTemplate:cpp/iterator/dcl list istreambuf iteratorTemplate:cpp/iterator/dcl list ostreambuf iterator

Iterator operations

Template:cpp/iterator/dcl list advanceTemplate:cpp/iterator/dcl list distanceTemplate:cpp/iterator/dcl list nextTemplate:cpp/iterator/dcl list prev
Defined in header <iterator>

Range access

Template:cpp/iterator/dcl list beginTemplate:cpp/iterator/dcl list end
Defined in header <iterator>