1、Iterator——提供一种方法访问一个容器对象中各个元素,而又无需暴露该对象的内部细节
#include <iostream> #include <vector> #include <numeric> using namespace std; int main () { vector<int> vInt(5); iota (vInt.begin(),vInt.end(),1); vector<int>::iterator It = vInt.begin(); while ( It != vInt.end() ) cout << It++ << " "; cout << ; // or for ( It = vInt.begin(); It != vInt.end(); It++ ) cout << It << " "; cout << ; return 0; } 运行结果: // 1 2 3 4 5 // 1 2 3 4 5
2、istream_iterator——专门针对标准输入流的迭代器
#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; int main () { //第一种运用方法:定义一个int类型的迭代器 istream_iterator<int> start_cin(cin); istream_iterator<int> end_of_cin; vector<int> v; cout << "Enter sequance of integers " << "(d - quit) : "; copy(start_cin,end_of_cin,back_inserter(v)); for ( vector<int>::iterator It = v.begin(); It != v.end(); It++ ) cout << *It << " - "; cout << endl; //第二种方法:定义一种string类型的迭代器 vector<string> vS; cout << "Enter three strings : "; for ( int i = 0; i < 3; i++ ) vS.push_back(*istream_iterator<string>(cin)); ostream_iterator<string> sIt(cout,", "); copy(vS.begin(),vS.end(),sIt); cout << endl; return 0; } 运行结果: // Enter sequance of integers (d - quit) : 2 3 4 // 2 - 3 - 4 - // Enter three strings : one two three // one, two, three,
3、ostream_iterator——专门针对标准输出流的迭代器
#include <iostream> #include <vector> #include <algorithm> #include <fstream> using namespace std; int main () { int ary[] = {2,5,7,2,8,9}; //以输出方式打开或新建文件 ofstream ofile("TEST.DAT"); // 写入标准输出 copy(ary,ary+6,ostream_iterator<int>(cout," - ")); // 写入 "TEST.DAT" 文件中 copy(ary,ary+6,ostream_iterator<int>(ofile,"\n")); ofile.close(); cout << endl; //以输入方式打开或新建文件 ifstream ifile("TEST.DAT"); istream_iterator<int> start_file(ifile); istream_iterator<int> end_file; // 从文件读取内容写入到标准输出 copy(start_file,end_file, ostream_iterator<int>(cout," * ")); ifile.close(); cout << endl; return 0; } 运行结果: // 2 - 5 - 7 - 2 - 8 - 9 - // 2 * 5 * 7 * 2 * 8 * 9 *
4、insert_iterator——用于定义一个可自增减并赋值的迭代器对象
#include <iostream> #include <list> #include <algorithm> using namespace std; int main () { list<int> L; L.push_front(3); insert_iterator<list<int> > ii(L, L.begin()); *ii++ = 0; *ii++ = 1; *ii++ = 2; copy(L.begin(), L.end(), ostream_iterator<int>(cout, " ")); } 运行结果: // 0 1 2 3.
5、inserter——向指定的容器中迭代插入元素,使用insert实现插入操作,需指定第二参数指向插入起始位置的迭代器
#include <iostream> #include <set> #include <algorithm> using namespace std; int main() { int a[] = {1, 3, 5, 7, 9, 10}; int b[] = {1, 2, 3, 4, 5, 6}; set<int> result; //merge合并两个有序的sequence,默认为<比较,可自定义比较函数comp, 结果保存在[result, result + (last1 - first1) + (last2 - first2)) 中。 merge(a, a+6, b, b+6, inserter(result, result.begin())); copy(result.begin(), result.end(), ostream_iterator<int>(cout, " ")); cout << endl; return 0; } 运行结果: // 1 2 3 4 5 6 7 9 10
6、front_inserter——向指定的容器中迭代插入元素,使用push_front 实现插入
#include <iostream> #include <deque> #include <algorithm> using namespace std; int main() { deque<int> d; front_inserter(d) = 100; front_inserter(d) = 200; copy(d.begin(),d.end(), ostream_iterator<int>(cout," ")); cout << endl; // 用 front_inserter 再次插入所有元素 copy (d.begin(), d.end(), front_inserter(d)); copy(d.begin(),d.end(), ostream_iterator<int>(cout," ")); cout << endl; return 0; } 运行结果: // 200 100 // 200 100 200 100
除非注明,文章均为CppLive 编程在线原创,转载请注明出处,谢谢。