对于C++来说
迭代器的使用类似于指针的使用
但却有着更好的操作
下面代码为迭代器测试代码
仅供自己学习使用
代码如下
//迭代器测试
#include
#include
#include
using namespace std;
#if 0
int main()
{
vector
v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector
::iterator first = v.begin(); vector
::iterator second = v.begin() + 1; vector
::iterator last = v.end() - 1; if (!v.empty()) { while ((second) != (last)) { cout << (*first) + (*second) << endl; first++; second++; } } cout << "-------------------------------------" << endl; system("pause"); return 0; } #endif #if 1 int main() { vector
v1; for (int i = 0; i < 9; i++) { v1.push_back(i); } vector
::iterator first = v1.begin(); vector
::iterator last = v1.end(); while (first != last && first < last) { cout << (*first) + (*(last-1)) << endl; first++; last--; } system("pause"); return 0; } #endif