STL Iterator
STL Iterator
每个容器定义自己的iterator类型,所以不用包含额外的头文件。
一些特殊类型的iterator和一些关于iterator的辅助操作需要头文件<iterator>
。
Iterators are objects that can iterate over elements of a sequence via a common interface that is adapted from ordinary pointers.
Iterator Categories
Output Iterators
Output iterators can only step forward with write access.
不能为同一个位置赋值两次。
Expression | Effect |
---|---|
*iter = val | Writes val to where the iterator refers |
++iter | Steps forward (returns new position) |
iter++ | Steps forward (returns old position) |
TYPE(iter) | Copies iterator (copy constructor) |
1 | OutputIterator pos; |
Input Iterators
Input iterators can only step forward element-by-element with read access.
不能读同一个位置两次。
==操作只能用于判断是否到末尾,指向不同位置的iterator也可能相等。
Expression | Effect |
---|---|
*iter | Provides read access to the actual element |
iter->member | Provides read access to a member of the actual element |
++iter | Steps forward (returns new position) |
iter++ | Steps forward |
iter1 == iter2 | Returns whether two iterators are equal |
iter1 != iter2 | Returns whether two iterators are not equal |
TYPE(iter) | Copies iterator (copy constructor) |
1 | InputIterator pos, end; |
Forward Iterators
Forward iterators are input iterators that provide additional guarantees while reading forward.
保证了两个指向同一个元素的iterator相等。
Expression | Effect |
---|---|
*iter | Provides access to the actual element |
iter->member | Provides access to a member of the actual element |
++iter | Steps forward (returns new position) |
iter++ | Steps forward (returns old position) |
iter1 == iter2 | Returns whether two iterators are equal |
iter1 != iter2 | Returns whether two iterators are not equal |
TYPE() | Creates iterator (default constructor) |
TYPE(iter) | Copies iterator (copy constructor) |
iter1 = iter2 | Assigns an iterator |
1 | ForwardIterator pos1, pos2; |
Bidirectional Iterators
Bidirectional iterators are forward iterators that provide the additional ability to iterate backward over the elements.
可以反向移动。
Expression | Effect |
---|---|
*iter | Provides access to the actual element |
iter->member | Provides access to a member of the actual element |
++iter | Steps forward (returns new position) |
iter++ | Steps forward (returns old position) |
iter1 == iter2 | Returns whether two iterators are equal |
iter1 != iter2 | Returns whether two iterators are not equal |
TYPE() | Creates iterator (default constructor) |
TYPE(iter) | Copies iterator (copy constructor) |
iter1 = iter2 | Assigns an iterator |
–iter | Steps backward (returns new position) |
iter– | Steps backward (returns old position) |
Random-Access Iterators
Random-access iterators provide all the abilities of bidirectional iterators plus random access.
可以对iterator进行算数操作,增减位移,计算差值,比较iterator。
Expression | Effect |
---|---|
*iter | Provides access to the actual element |
iter->member | Provides access to a member of the actual element |
++iter | Steps forward (returns new position) |
iter++ | Steps forward (returns old position) |
iter1 == iter2 | Returns whether two iterators are equal |
iter1 != iter2 | Returns whether two iterators are not equal |
TYPE() | Creates iterator (default constructor) |
TYPE(iter) | Copies iterator (copy constructor) |
iter1 = iter2 | Assigns an iterator |
–iter | Steps backward (returns new position) |
iter– | Steps backward (returns old position) |
iter[n] | Provides access to the element that has index n |
iter+=n | Steps n elements forward (or backward, if n is negative) |
iter-=n | Steps n elements backward (or forward, if n is negative) |
iter+n | Returns the iterator of the nth next element |
n+iter | Returns the iterator of the nth next element |
iter-n | Returns the iterator of the nth previous element |
iter1-iter2 | Returns the distance between iter1 and iter2 |
iter1<iter2 | Returns whether iter1 is before iter2 |
iter1>iter2 | Returns whether iter1 is after iter2 |
iter1<=iter2 | Returns whether iter1 is not after iter2 |
iter1>=iter2 | Returns whether iter1 is not before iter2 |
1 |
|
Auxiliary Iterator Functions
advance(); next(); prev(); distance(); iter_swap();
给其他一些类型的iterator一些random-access iterator才有的功能。
advance()
The function advance() increments the position of an iterator passed as the argument.
让iterator向前或向后移动几个元素。
1 |
|
- 让input iterator可以向前(或向后)移动n个元素。
- 对于bidirectional和random-access的iterator,n可以是负值,从而向后移动。
- Dist是一个模板类型。一般都是整型。
- advance()不检查是否越过序列结尾。因为迭代器不知道容器里的情况。
- 一般应该用advance()而不是+=,方便修改容器。不过要注意可能的效率降低,以及返回值的不同(advance没有返回值,+=返回新位置)。
next() and prev()
C++11后才有。
1 |
|
- Dist的类型是std::iterator_traits
::difference_type。 - 内部调用advance(pos, n)。
1 |
|
- Dist的类型是std::iterator_traits
::difference_type。 - 内部调用advance(pos, -n)。
distance()
The distance() function is provided to process the difference between two iterators.
1 |
|
- 两个迭代器需指向同一个容器。
- 如果不是random-access迭代器,pos2必须等于或大于pos1,即从pos1可以到达pos2。
- Dist的类型是iterator_traits
::difference_type。
iter_swap()
The iter_swap() function is provided to swap the values to which two iterators refer.
1 |
|
- 交换pos1和pos2所指向的值。
- 两个迭代器可以是不同类型,只要所指向的元素是可以修改的。
Iterator Adapters
Reverse Iterators
只有Forward List和Unordered Container不提供。
rbegin() 返回反向的第一个元素的位置。
rend() 返回反向的最后一个元素之后的位置。
当普通iterator和reverse iterator相互转换时要注意,iterator需要是bidirectional iterator,并且他们的逻辑位置并不是对应的。
如{1,2,3,4,5,6,7}:
iter指向5,值也是5;riter指向5,值是4。物理位置相同,逻辑值不同。
如果iter1, iter2表示[2,5)的区间,即2,3,4。
如果riter1, riter2表示[4,1)的区间,即4,3,2。
区间还是相同的。
相互转化:
1 | list<int>::iterator pos; |
pos: 5; rpos: 4; rrpos: 5;
Insert Iterators
把对当前位置的赋值变成在当前位置插入。从而可以插入而不是覆盖。
三种类型。back inserter, front inserter, general inserter
。
容器必须提供called function,所以:
- back inserter只能用于vector, deque, list, string。
- front inserter只能用于deque, list。
- general inserter除了array和forward list都可以用。因为需要insert()成员函数。
Back Inserters
1 | vector<int> coll; |
Front Inserters
1 | list<int> coll; |
注意front inserter插入后顺序是反向的。
General Inserters
每次插入完成后,insert会自动++。
1 | set<int> coll; |
Stream Iterators
A stream iterator is an iterator adapter that allows you to use a stream as a source of destination of algorithms.
Ostream Iterators
Ostream iterators write assigned values to an output stream.
1 | string delim("|"); |
输出1|2|3|4|5|6|7|
Istream Iterators
Istream iterators reads elements from an input stream.
1 | // create istream iterator that reads integers from cin |
Example of Stream Iterators and advance()
1 |
|
如果输入:No one objects if you are doing a good programming job for someone whom you respect.
会输出:objects are good for you
[1] The C++ Standard Library 2nd Edition