Logo

Programming-Idioms

History of Idiom 7 > diff from v64 to v65

Edit summary for version 65 by rpm:
[Cpp] Not idiomatic

Version 64

2019-09-26, 14:35:42

Version 65

2019-09-26, 14:56:49

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

Extra Keywords
traverse traversal
Extra Keywords
traverse traversal
Imports
#include <iostream>
#include <vector>
Imports
#include <iostream>
#include <vector>
Code
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". Index is initialized to zero only once for the entire program regardless of how many times this loop is run. Also not thread safe.
Doc URL
http://en.cppreference.com/w/cpp/language/range-for
Doc URL
http://en.cppreference.com/w/cpp/language/range-for