#include // std::cout #include // std::vector, std::next namespace{ int N = 100; } /* * example for iterating over the elements of an STL container */ int main() { std::vector v{}; for(int i = 1; i <= N; i++) v.push_back(i); // 1) // using an explicit iterator type int sum = 0; for(std::vector::iterator j = v.begin(); j != v.end(); j = std::next(j)) sum += *j; std::cout << sum << "\n"; // 2) // implicit typing with auto - this is a common use case for "auto" sum = 0; for(auto j = v.begin(); j != v.end(); j = std::next(j)) sum += *j; std::cout << sum << "\n"; // 3) // more recently introduced syntax; note that this copies the values into "i" sum = 0; for(int i: v) sum += i; std::cout << sum << "\n"; }