Answers for "Initialize Vector Iterator"

C++
0

Initialize Vector Iterator

#include <iostream>
#include <vector>
using namespace std;

int main() {
  vector<int> num {1, 2, 3, 4, 5};

  // declare iterator
  vector<int>::iterator iter;

  // initialize the iterator with the first element
  iter = num.begin();

  // print the vector element
  cout << "num[0] = " << *iter << endl;

  // iterator points to the 4th element
  iter = num.begin() + 3;
  cout << "num[3] = " << *iter << endl;

  // iterator points to the last element
  iter = num.end() - 1;
  cout << "num[4] = " << *iter;

  return 0;
}
Posted by: Guest on May-05-2022

Browse Popular Code Answers by Language