iterator

本文详细介绍了C++中迭代器的概念及其五种类别:随机访问迭代器、双向迭代器、前向迭代器、输入迭代器和输出迭代器。此外,还列举了不同类别迭代器所支持的操作,并提供了各类迭代器特性的对比。

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).

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:

     

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 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 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


Functions

Iterator operations:

Inserters:

Predefined iterators


Inserter iterators
Input/Output iterators
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值