网站建设维护工作,服务器在国外未备案网站,可信网站认证有用,中国甘肃网1.(P252) 迭代器的分类及其能力#xff1a;input迭代器只能读取元素一次。如果复制input迭代器#xff0c;并使原迭代器和新产生副本都向前读取#xff0c;可能会遍历到不同的值。output迭代器类似。 2.(P258) C不允许修改任何基本类型#xff08;包括指针#xff09;的暂…1.(P252) 迭代器的分类及其能力input迭代器只能读取元素一次。如果复制input迭代器并使原迭代器和新产生副本都向前读取可能会遍历到不同的值。output迭代器类似。 2.(P258) C不允许修改任何基本类型包括指针的暂时值但对于struct, class则允许。 所以 1
2
vectorint ivec;
sort(ivec.begin(), ivec.end()); 也许会失败这取决于vector的实作版本。 3.(P259) C标注库为迭代器提供的三个辅助函数①. advance() 前进(或后退)多个元素 1
2
#include iterator
void advance(InputIterator pos, Dist n) 注对于Bidirectional迭代器或Random Access迭代器n可以为负值表示后退②. distance() 处理迭代器之间的距离 1
2
#include iterator
Dist distance (InputIterator pos1, InputIterator pos2) ③. iter_swap() 可交换两个迭代器所指内容 1
2
#include algorithm
void iter_swap(ForwardIterator pos1, ForwardIterator pos2) 注不是交换迭代器 4.(P264) 迭代器配接器之Reverse(逆向)迭代器对于reverse iterator,他实际所指位置与逻辑所指位置并不一样eg. 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include iostream
#include vector
#include algorithm
using namespace std; int main()
{ vectorint coll; for (int i1; i9; i) { coll.push_back(i); } vectorint::iterator pos; pos find (coll.begin(), coll.end(),5); cout pos: *pos endl; vectorint::reverse_iterator rpos(pos); cout rpos: *rpos endl;
} 结果是pos: 5 rpos: 4 这是reverse iterator的内部机理图(*) 可以看出[begin(), end() ) 和 [rbegin(), rend() )的区间是一样的base()函数可以将逆向迭代器转回正常迭代器 eg. 1
pos rpos.base(); 注 1
vectorint::reverse_iterator rpos(pos); 可以将迭代器赋值给逆向迭代器从而隐式转换而将逆向迭代器转换成普通迭代器则只能用base()函数。这一块内容颇多要认真把P264~P270看看。 5.(P271) 迭代器配接器之Insert(安插)迭代器 6.(P277) 迭代器配接器之Stream(流)迭代器Osream流迭代器Istream流迭代器综合性的代码 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Author: Tanky Woo
// Blog: www.WuTianQi.com
#include iostream
#include vector
#include iteratorusing namespace std; int main()
{ istream_iteratorint cinPos(cin); istream_iteratorint cinEnd; ostream_iteratorint coutPos(cout, ); while(cinPos ! cinEnd) *coutPos *cinPos; return 0;
}转载于:https://www.cnblogs.com/tanky_woo/archive/2011/01/29/1947547.html