您的位置:首页 > 其它

STL中标准的迭代器接口

2012-08-16 15:15 99 查看
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 types
iterator_category
(a synonym for the template parameter
C
),
value_type
(a synonym for the template parameter
T
), and
distance_type
(a synonym for the template parameter
Dist
).

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 a
container), 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, each
container 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 different
iterator 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 of
input 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. All
standard containers support at least bidirectional iterators types.

Random access iterators implement all the functionalities of
bidirectional 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 expressions
all 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 iterators
a < b

a > b

a <= b

a >= b
Supports compound assignment operations += and -=a += n

a -= n
Supports offset dereference operator ([])a
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 of
random access iterators's.
Forward iterators have a subset of
bidirectional iterators's. And
input and
output have each their own subset of
forward iterator's.

Base

iteratorIterator base class (class template)
iterator_traitsIterator traits (class template)

Functions

Iterator operations:

advanceAdvance iterator (function template)
distanceReturn distance between iterators (function template )

Inserters:

back_inserterConstruct a back insert iterator (function template)
front_inserterConstructs a front insert iterator (function template)
inserterConstruct an insert iterator (function template)

Predefined iterators

reverse_iteratorReverse iterator (class template)

Inserter iterators

back_insert_iteratorBack insert iterator (class template )
front_insert_iteratorFront insert iterator (class template)
insert_iteratorInsert iterator (class template)

Input/Output iterators

istream_iteratorIstream iterator (class template)
ostream_iteratorOstream iterator (class template )
istreambuf_iteratorInput stream buffer iterator (class template)
ostreambuf_iteratorOutput stream buffer iterator (class template)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: