C++ 迭代器类型

There are five (until C++17)six (since C++17) kinds of iterators: InputIteratorOutputIteratorForwardIterator,BidirectionalIteratorRandomAccessIterator, and ContiguousIterator (since C++17).


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.

All of the iterator categories (except OutputIterator) 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 ofOutputIterator, then it is called a mutable iterator and supports both input and output. Non-mutable iterators are called constant iterators.


C++17 以前,有5种类型的迭代器;C++17后有6种类型

输入迭代器        读,不能写,只支持自增

输出迭代器        写,不能读,只支持自增

前向迭代器        读和写,只支持自增

双向迭代器         读和写,支持自增和自减

随机访问迭代器     都和写,支持完整的迭代器算术运算;



根据迭代支持的操作来定义迭代器,只要某一个类型满足了某一种迭代器所需的操作,就可以被认为是该中迭代器

迭代器分类是根据读写和访问方向两个维度划分的


常见的输入迭代器是istream::iterator

常见的输出迭代器是ostream::iterator


所有标准库容器提供的迭代器至少达到双向迭代器要求,vector,string,deque提供的随机访问迭代器,用作访问内置数组的指针也是随机访问迭代器


关联容器map,set 虽然提供双向迭代器,但是由于key 是const 对象,因此关联容器不能使用任何写序列的算法,故在处理算法时,可以将关联容器的迭代器视为支持自减的输入迭代器,而不是完整的双向迭代器


copy find,accumulate算法需要输入迭代器

reverse 算法需要双向迭代器,unuque算法需要前向迭代器



http://en.cppreference.com/w/cpp/iterator

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值