除了为每个容器定义的迭代器之外,标准库在头文件 iterator 中还定义了额外几种迭代器。这些迭代器包括以下几种:

插入迭代器:这些迭代器被绑定到一个容器上,可用来向容器插入元素

流迭代器:这些迭代器被绑定到输入或输出流上,可用来变量所有管理的 IO 流

反向迭代器:这些迭代器向后而不是向前移动。除了 forward_list 之外的标准库容器都有反向迭代器

移动迭代器:这些专用的迭代器不是拷贝其中的元素,而是移动它们。

 

插入迭代器(back_inserter/front_inserter/inserter):

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <iterator>    
 4 #include <list>
 5 using namespace std;
 6 
 7 int main(void){
 8     list<int> lst = {1, 2, 3, 4, 5};
 9     list<int> lst1, lst2, lst3;//空list
10     
11     copy(lst.cbegin(), lst.cend(), front_inserter(lst1));
12     for(const auto &indx : lst1){
13         cout << indx << " ";
14     }
15     cout << endl;//5 4 3 2 1
16 
17     copy(lst.cbegin(), lst.cend(), back_inserter(lst2));
18     for(const auto &indx : lst2){
19         cout << indx << " ";
20     }
21     cout << endl;//1 2 3 4 5
22 
23     copy(lst.cbegin(), lst.cend(), inserter(lst3, lst3.begin()));
24     for(const auto &indx : lst3){
25         cout << indx << " ";
26     }
27     cout << endl;//1 2 3 4 5
28 
29     return 0;
30 }
View Code

相关文章: