Iterator Adapter
迭代器适配器,特殊类型的迭代器,其内部可能重新封装了iterator operator相关的成员函数,从外部来看,依旧保留了所有迭代器的特性,赋予了迭代器抽象更加强大的威力;
1.Insert Iterator //安插型迭代器
2.Stream Iterator //串流迭代器
3.Reverse Iterator //逆向迭代器
4.Move Iterator //搬移迭代器
Insert Iterator
# include <iostream>
# include <iterator>
# include <list>
# include <set>
# include <vector>
# include <algorithm>
int main()
{
list<int> col1 = {1,2,3,4,5,6,7,8,9};
vector<int> col2;
// we need copy col1 to col2;
// method 1;需要提前知道容器大小
col2.resize(col1.size());
copy(col1.begin(),col1.end(),col2.begin();
// method 2;调用了容器.push_back()方法;
copy(col1.begin(),col1.end(),back_inserter(col2));
//method 3;调用了容器.insert(pos)方法;
copy(col1.begin(),col1.end(),inserter(col12));
//method4:调用了容器.push_front()方法
copy(col1.rbegin(),col2.rend(),pop_inserter(col2));
}
insert iterator | back_inserter | front_inserter | inserter |
---|---|---|---|
container | vector,list,deque,string | deque,list,forward_list | all |
stream iterator
用来读写stream,使得来自键盘的输入像是个集合,能够从中读取内容,同样道理,也可以把算法的输出结果重新导向到某个文件和屏幕(理解为容器)。
# include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <iterator>
using namespace std;
int main()
{
vector<string> col1;
//从标准输入读取字符到col1中,读取以空白分割的字符串(word by word)
//istream_iterator<string>(),表示“串流结束符”(end of stream)
copy(istream_iterator<string>(cin),istream_iterator<string>(),back_inserter(col1));
sort(col1.begin(),col1.end());
// 把col1输出到标准输出上(屏幕),不输出重复的字符串
unique_copy(col1.begin(),col1.end(),ostream_iterator<string>(cout,'\n'));
}
三条语句就完成了从标准输入->vector<string>->排序->标准输出(ctrl+z结束输入);其内部实现大致揣测如下;
template<class T, class Distance = ptrdiff_T>
class istream_iterator{
freind bool;
operator == __STL_NULL_TMPL_ARGS(const istream_iterator<T,Distance>&x,
const istream_iterator<T,Distance>&y);
ptotected:
istream * stream;
T value;
bool end_marker;
void read(){
end_marker = (*stream)?true:false;
if(end_marker)
*stream >> value;
end_marker = (*stream)? true:false;
}
public:
typedef input_iterator_tag iterator_category;
typedef T vaue_type;
typedef Distance difference_type;
typedef T* pointer;
typedef T& reference ;
istream_iterator():stream(&cin),end_marker(false){};
istream_iterator(istream& s):stream(&s){read();}
reference operator*() const { return value};
isream_iterator<T, Distance>& operator++(){
read();
return *this;
};
istream_iterator<T,Distance> operator++(int) {
istream_iterator<T,Distance> tmp = *this;
read();
return tmp;
};
}
ostream_iterator<T>的实现如下(实现相对简单):
template<typename T,class Distance = ptrdiff_T>
class ostream_iterator
{
public:
typedef input_iterator_tag iterator_category;
typedef T vaue_type;
typedef Distance difference_type;
typedef T* pointer;
typedef T& reference ;
ostream_iterator(ostream os,string str):_os(os),_str(str){}:
ostream_iterator& operator*() { return *this;};
void operator++ (){};
void operator= (T t){_os << t << str;}
private:
ostream _os;
string _str;
}
Reverse Iterator
提供算法的逆向操作,将incresement 转变为decrement。所有提供双向迭代器的容器,都可以通过rbegin()和rend()产生一个反向迭代器,以及crbegin()和crend()以只读的方式产生反向迭代器。
# include <iterator>
# include <algorithm>
# include <vector>
# include <iosream>
using namespace std;
int main()
{
vector<int> col1;
for(int i = 0; i < 9; ++i) col1.push_back(i);
copy(col1.rbegin(),col1.rend(),ostream_iterator<int>(cout,","));
cout << endl;
}
// rbegin()内部重载了++操作;
Move Iterator
用来将任何“对底层元素”的访问转换为“move操作”,始自c++11;