初始化:
list<char>::iterator pos;
算法:
1. 遍历
for(pos = col1.begin(); pos != col1.end(); ++pos){...}
代码:
1 #include <iostream> 2 #include <vector> 3 #include <deque> 4 #include <set> 5 #include <algorithm> 6 #include <iterator> //for ostream_iterator&istream_iterator 7 #include "utility.h" 8 using namespace std; 9 10 int main() { 11 vector<int> v; 12 vector<int>::iterator pos; 13 14 for(int i = 6; i >= 1; i--) 15 v.push_back(i); 16 17 //back_inserter调用push_back(), 适用于vector, deque, list 18 vector<int> col1; 19 copy(v.begin(), v.end(), back_inserter(col1)); 20 PRINT_ELEMENT(col1); 21 22 //front_inserter调用push_front(),适用于deque, list 23 deque<int> col2; 24 copy(v.begin(), v.end(), front_inserter(col2)); 25 PRINT_ELEMENT(col2); 26 27 //inserter调用inserter(), 适用于所有容器 28 set<int> col3; 29 copy(v.begin(), v.end(), inserter(col3, col3.end())); 30 PRINT_ELEMENT(col3); 31 32 //ostream iterator 33 copy(col3.rbegin(), col3.rend(), ostream_iterator<int>(cout, " ")); 34 cout << endl; 35 36 //istream iterator 37 vector<string> col4; 38 copy(istream_iterator<string>(cin), istream_iterator<string>(), back_inserter(col4)); 39 PRINT_ELEMENT(col4); 40 return 0; 41 }
输出:
$ ./a.exe content: 6 5 4 3 2 1 content: 1 2 3 4 5 6 content: 1 2 3 4 5 6 6 5 4 3 2 1 abd adfasdf assdfasdf afewaf 1a `1111 adsfasdf content: abd adfasdf assdfasdf afewaf 1a `1111 adsfasdf