how to iterate over a vector using an iterator code example

Example 1: c++ iterate through vectgor

std::vector<int> vec_of_ints(100);
for (int i = 0; i < vec_of_ints.size(); i++){
 	std::cout << vec_of_ints.at(i) << " "; 
}
std::cout << std::endl;

Example 2: c++ vector iterator

// EXAMPLE
vector<string> vData;
vData.push_back("zeroth");
vData.push_back("first");
vData.push_back("second");
vData.push_back("third");

std::vector<string>::iterator itData;

for (itData = vData.begin(); itData != vData.end() ; itData++)
{
  auto ElementIndex = itData-vData.begin();
  auto ElementValue = vData[ElementIndex]; // vData[ElementIndex] = *itData
  cout << "[ElementIndex:" << ElementIndex << "][ElementValue:" << ElementValue << "]\n";
}

/* HEADER(S)
#include <vector>
#include <iostream>
using namespace std;
*/

Tags:

Cpp Example