Logo

Programming-Idioms

History of Idiom 7 > diff from v33 to v34

Edit summary for version 34 by :
New Cpp implementation by user [mikee]

Version 33

2015-12-30, 17:02:40

Version 34

2016-01-19, 09:59:42

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Imports
#include <iostream>
#include <vector>
Code
std::vector<int> items = {0, 1, 2, 3, 4, 5};

for(const auto & item : items) {
  static auto idx = 0;
  std::cout << "Item " << idx++ << " = " << item << std::endl;
}
Comments bubble
Uses the C++11 features "range based for loop" and "auto"