[C++] c++ 在一个向量使用指针语义(迭代器)访问元素 →→→→→进入此内容的聊天室

来自 , 2020-10-12, 写在 C++, 查看 153 次.
URL http://www.code666.cn/view/f45a1078
  1. // 在一个向量使用指针语义(迭代器)访问元素
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. int main() {
  6.         using namespace std;
  7.  
  8.         vector<int> vecDynamicIntegerArray;
  9.  
  10.         // Insert sample integers into the vector:
  11.         vecDynamicIntegerArray.push_back(50);
  12.         vecDynamicIntegerArray.push_back(1);
  13.         vecDynamicIntegerArray.push_back(987);
  14.         vecDynamicIntegerArray.push_back(1001);
  15.  
  16.         // Access objects in a vector using iterators:
  17.         vector<int>::iterator iElementLocator = vecDynamicIntegerArray.begin();
  18.  
  19.         while (iElementLocator != vecDynamicIntegerArray.end()) {
  20.                 size_t nElementIndex = distance(vecDynamicIntegerArray.begin(),
  21.                                 iElementLocator);
  22.  
  23.                 cout << "Element at position ";
  24.                 cout << nElementIndex << " is: " << *iElementLocator << endl;
  25.  
  26.                 // move to the next element
  27.                 ++iElementLocator;
  28.         }
  29.  
  30.         return 0;
  31. }
  32.  

回复 "c++ 在一个向量使用指针语义(迭代器)访问元素"

这儿你可以回复上面这条便签

captcha