Iterator library
Iterator库
迭代器库为五种迭代器以及迭代器的特性、适配器和实用程序函数提供了定义。
Iterator类别
有5%28直到C++17%296%28,因为C++17%29种迭代器:InputIterator
,,,OutputIterator
,,,ForwardIterator
,,,BidirectionalIterator
,,,RandomAccessIterator
,和ContiguousIterator
%28自C++17%29。
每个迭代器类别不是由特定类型定义的,而是由可以在其上执行的操作定义的。此定义意味着任何支持必要操作的类型都可以用作迭代器--例如,指针支持RandomAccessIterator
,因此指针可以在RandomAccessIterator
是预料中的。
所有迭代器类别%28除外OutputIterator
%29可以组织成一个层次结构,其中更强大的迭代器类别%28例如。RandomAccessIterator
%29支持功能较弱的类别的操作。InputIterator
29%。如果迭代器属于这些类别之一,并且也满足OutputIterator
,则称为可变
迭代器和支持双双
输入和输出。不可变
迭代器称为常量
迭代器。
Iterator category | Defined operations |
---|---|
ContiguousIterator | RandomAccessIterator |
| increment (with multiple passes) |
| decrement |
| random access |
| contiguous storage |
属于上述类别之一并满足OutputIterator要求的迭代器称为可变迭代器。
输出迭代器写入增量%28而不多次传递%29
- 读
- 增量%28没有多次传球%29
- 多次传球增量%28%29
- 减量
- 随机存取
- 连续存储
Iterators that fall into one of the above categories and also meet the requirements of [`OutputIterator`](concept/outputiterator) are called mutable iterators.
[`OutputIterator`](concept/outputiterator)
- 写
- 增量%28没有多次传球%29
注:ContiguousIterator
类别只在C++17中正式指定,但是std::vector
,,,std::basic_string
,,,std::array
,和std::valarray
,以及指向C数组的指针在C++17代码中通常被视为一个单独的类别。
迭代原语
iterator_traits | provides uniform interface to the properties of an iterator (class template) |
---|---|
input_iterator_tagoutput_iterator_tagforward_iterator_tagbidirectional_iterator_tagrandom_access_iterator_tag | empty class types used to indicate iterator categories (class) |
iterator (deprecated in C++17) | base class to ease the definition of required types for simple iterators (class template) |
迭代器适配器
reverse_iterator | iterator adaptor for reverse-order traversal (class template) |
---|---|
make_reverse_iterator (C++14) | creates a std::reverse_iterator of type inferred from the argument (function template) |
move_iterator (C++11) | iterator adaptor which dereferences to an rvalue reference (class template) |
make_move_iterator (C++11) | creates a std::move_iterator of type inferred from the argument (function template) |
back_insert_iterator | iterator adaptor for insertion at the end of a container (class template) |
back_inserter | creates a std::back_insert_iterator of type inferred from the argument (function template) |
front_insert_iterator | iterator adaptor for insertion at the front of a container (class template) |
front_inserter | creates a std::front_insert_iterator of type inferred from the argument (function template) |
insert_iterator | iterator adaptor for insertion into a container (class template) |
inserter | creates a std::insert_iterator of type inferred from the argument (function template) |
流迭代器
istream_iterator | input iterator that reads from std::basic_istream (class template) |
---|---|
ostream_iterator | output iterator that writes to std::basic_ostream (class template) |
istreambuf_iterator | input iterator that reads from std::basic_streambuf (class template) |
ostreambuf_iterator | output iterator that writes to std::basic_streambuf (class template) |
迭代操作
在标头中定义<iterator>
*。
将迭代器前进给定距离%28功能%29
距离返回两个迭代器之间的距离%28函数%29
下一个%28C++11%29增量迭代器%28功能%29
PRV%28C++11%29减少迭代器%28功能%29
范围存取
这些非成员函数为容器、普通数组和std::initializer_list
...
在标头中定义<iterator>
*。
初学者%28C++11%29%28C++14%29返回一个迭代器到容器的开头或数组%28函数%29
endcend%28C++11%29%28C++14%29将迭代器返回到容器的末尾或数组%28函数%29
返回反向迭代器到容器或数组%28函数%29
rendcrend%28C++14%29返回容器的反向迭代器或数组%28功能%29
集装箱存取
这些非成员函数为容器、普通数组和std::initializer_list
...
size (C++17) | returns the size of a container or array (function) |
---|---|
empty (C++17) | checks whether the container is empty (function) |
data (C++17) | obtains the pointer to the underlying array (function) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。