让我们看一个简单的示例:
#include <iostream> #include <fstream> #include <iterator> #include <vector> using namespace std; template<class ForwardIterator> //函数模板 void display(ForwardIterator first, ForwardIterator last) //display函数 { while(first!=last) { cout<<*first<<" "; first++; } } int main() { vector<int> a; //向量的声明。 for(int i=1;i<=10;i++) { a.push_back(i); } display(a.begin(),a.end()); //调用 display() 函数. return 0; }
输出:
1 2 3 4 5 6 7 8 9 10
假设" A"和" B"是两个迭代器:
A==B; //相等运算符 A!=B; //不相等运算符
假设" A"是迭代器," t"是整数变量:
*A = t; t = *A;
假设" A"是迭代器:
A++; ++A;
假设" A"是迭代器:
A--; //invalid
假设" A"和" B"是两个迭代器:
A==B; //valid A>=B; //invalid
A+2; //invalid A+3; //invalid
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)