STL中标准的迭代器接口

来源:互联网 发布:数控编程员工资 编辑:程序博客网 时间:2024/06/06 07:33
template<class C, class T, class Dist = ptrdiff_t>    struct iterator {    typedef C iterator_category;    typedef T value_type;    typedef Dist distance_type;    };

The template class serves as a base type for all iterators. It defines the member typesiterator_category (a synonym for the template parameterC), value_type (a synonym for the template parameterT), and distance_type (a synonym for the template parameterDist).

 

 

iterator

Iterator definitions//迭代器定义:

In C++, an iterator is any object that, pointing to some element in a range of elements (such as an array or acontainer), has the ability to iterate through the elements of that range using a set of operators (at least, the increment (++) and dereference (*) operators).
在c++中,迭代器就是指向特定范围中(比如数组或容器)的某些元素的对象,迭代器拥有通过使用一系列的操作符(至少,自增和解引用操作符)重复操作特定范围里的元素
The most obvious form of iterator is a pointer: A pointer can point to elements in an array, and can iterate through them using the increment operator (++). But other forms of iterators exist. For example, eachcontainer type (such as a vector) has a specific iterator type designed to iterate through its elements in an efficient way.

Notice that while a pointer is a form of iterator, not all iterators have the same functionality a pointer has; To distinguish between the requirements an iterator shall have for a specific algorithm, five differentiterator categories exist:

Iterator categories

Iterators are classified in five categories depending on the functionality they implement:

RandomAccess
Bidirectional
Forward
Input
     
Output


In this graph, each iterator category implements the functionalities of all categories to its right:

Input and output iterators are the most limited types of iterators, specialized in performing only sequential input or output operations.

Forward iterators have all the functionality ofinput and output iterators, although they are limited to one direction in which to iterate through a range.

Bidirectional iterators can be iterated through in both directions. Allstandard containers support at least bidirectional iterators types.

Random access iterators implement all the functionalities ofbidirectional iterators, plus, they have the ability to access ranges non-sequentially: offsets can be directly applied to these iterators without iterating through all the elements in between. This provides these iterators with the same functionality as standard pointers (pointers are iterators of this category).

The characteristics of each category of iterators are:

categorycharacteristicvalid expressionsall categoriesCan be copied and copy-constructedX b(a);
b = a;
Can be incremented++a
a++
*a++
Random AccessBidirectionalForwardInputAccepts equality/inequality comparisonsa == b
a != b
Can be dereferenced as an rvalue*a
a->m
OutputCan be dereferenced to be the left side of an assignment operation*a = t
*a++ = t
 Can be default-constructedX a;
X()
 Can be decremented--a
a--
*a--
 Supports arithmetic operators + and -a + n
n + a
a - n
a - b
Supports inequality comparisons (<, >, <= and >=) between iteratorsa < b
a > b
a <= b
a >= b
Supports compound assignment operations += and -=a += n
a -= n
Supports offset dereference operator ([])a[n]


Where X is an iterator type, a and b are objects of this iterator type,t is an object of the type pointed by the iterator type, and n is an integer value.

Random access iterators have all characteristics.Bidirectional iterators have a subset ofrandom access iterators's.Forward iterators have a subset ofbidirectional iterators's. Andinput and output have each their own subset of forward iterator's.

Base


 

Functions

Iterator operations:


Inserters:


 

Predefined iterators


Inserter iterators


Input/Output iterators