Iterator adapter

  • reverse_iterator:
如下图,end()rbegin()两个迭代器内部的指针起始是一样的,区别在于方向不同,导致取的值不同;
因此通过调用reverse_iterator的构造函数,将正向迭代器保存到cunrrent中,当真正调用*++等等成员函数时,对保存的current加以改造

使用方式:
vector<int> v;
...
v.rbegin(); //就会得到v.end()的逆行迭代器,对rbegin()进行操作时,实际底层是在v.end()(即current)的操作基础上进行改造

Insert picture description here

  • inserter iterator:
    There are three insert iterators: inserter(c, it); back_inserter©, front_inserter©;
    they all use data members to save the underlying container c (inserter also saves an additional insertion position it), and overload operator= , When assigning values ​​to these three iterators, insert, push_back, and push_front are called respectively on the underlying container. For other ++, * and other operations, it just returns back_insert_iterator itself directly, and does nothing else;
下图中在copy算法中对第三参数resuly做的事情:*, =, ++操作
operator*不做任何事情,直接返回本身
operator=会对底层容器调用insert操作
operator++不做任何事情,直接返回本身

Insert picture description here

  • stream_iterator:istream_iterator和ostream_iteraor
//调用ostream_iterator的构造函数,将输出流cout和分隔符,保存在其数据成员中
//当对ostream_iterator做赋值操作时,ostream_iterator重载的operator=内作cout << *first; cout << ",";操作
ostream_iterator<int> out_it(cout, ","); 

如下图,operator*,operator++同样不做任何事情,只有operator=内分别对保存的数据成员调用<<

Insert picture description here

istream_iterator<double> eos;//iit==eos代表输入结束了
istream_iterator<double> iit(cin);

Insert picture description here

Guess you like

Origin blog.csdn.net/jiuri1005/article/details/114630562