1 #include "stdafx.h" 2 #include<iostream> 3 #include<iterator> 4 #include<list> 5 6 7 using namespace std; 8 9 int iArray[5] = { 1, 2, 3, 4, 5 }; 10 void Display(list<int>&v, const char*s); 11 int main() 12 { 13 list<int> iList; //定义了一个列表容器iList 14 copy(iArray, iArray + 5, front_inserter(iList)); //拷贝数组iArray到链表对象 15 Display(iList, "Before find and copy"); 16 list<int>::iterator p = find(iList.begin(), iList.end(), 3); //将适配器指向列表中的第三个数 17 copy(iArray, iArray + 2, inserter(iList, p)); 18 Display(iList, "After find and copy"); 19 return 0; 20 } 21 22 void Display(list<int>&a, const char*s) 23 { 24 cout << s << endl; 25 copy(a.begin(), a.end(), ostream_iterator<int>(cout, " ")); //输出迭代器使用空格符作为元素的分隔符 26 cout << endl; 27 }
再比如说 ostream_iterator<int> output(cout," "); //定义了一个output的输出迭代器
copy(vec.begin(),vec.end(),output); //以output的输出方式输出vector中的元素