Logo

Programming-Idioms

History of Idiom 7 > diff from v56 to v57

Edit summary for version 57 by programming-idioms.org:
[Cpp] +DocURL

Version 56

2016-11-27, 22:20:09

Version 57

2017-01-25, 09:35:12

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"
Doc URL
http://en.cppreference.com/w/cpp/language/range-for