how to loop through 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: iterate over vector in c++
for (auto & element : vector) {
element.doSomething ();
}
Example 3: c++ looping through a vector
for(std::vector<T>::size_type i = 0; i != v.size(); i++) {
v[i].doSomething();
}
Example 4: iterate on vector c++
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector;
for (int i=1; i<=5; i++) myvector.push_back(i);
std::cout << "myvector contains:";
for (std::vector<int>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}