[c++]代码库
// 在一个向量使用指针语义(迭代器)访问元素
#include <iostream>
#include <vector>
int main() {
using namespace std;
vector<int> vecDynamicIntegerArray;
// Insert sample integers into the vector:
vecDynamicIntegerArray.push_back(50);
vecDynamicIntegerArray.push_back(1);
vecDynamicIntegerArray.push_back(987);
vecDynamicIntegerArray.push_back(1001);
// Access objects in a vector using iterators:
vector<int>::iterator iElementLocator = vecDynamicIntegerArray.begin();
while (iElementLocator != vecDynamicIntegerArray.end()) {
size_t nElementIndex = distance(vecDynamicIntegerArray.begin(),
iElementLocator);
cout << "Element at position ";
cout << nElementIndex << " is: " << *iElementLocator << endl;
// move to the next element
++iElementLocator;
}
return 0;
}