Logo

Programming-Idioms

History of Idiom 7 > diff from v41 to v42

Edit summary for version 42 by :
[Cpp] items is supposed to be given. We focus on the code of the loop.

Version 41

2016-02-05, 10:53:00

Version 42

2016-02-05, 10:54:08

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>
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;
}
Code
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"
Comments bubble
Uses the C++11 features "range based for loop" and "auto"