how to iterate on vector in c++ 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

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

vector<int> myvector;

for (vector<int>::iterator it = myvector.begin();
     it != myvector.end();
     ++it)
   cout << ' ' << *it;
cout << '\n';

Example 3: c++ iterate over vector

for(auto const& value: a) {
     /* std::cout << value; ... */
}

Example 4: c++ looping through a vector

vector<int> vi;
...
for(int i : vi) 
  cout << "i = " << i << endl;

Tags:

Cpp Example