Logo

Programming-Idioms

History of Idiom 7 > diff from v147 to v148

Edit summary for version 148 by dstbstr:
New C++ implementation by user [dstbstr]

Version 147

2023-10-31, 21:23:37

Version 148

2024-04-29, 21:57:18

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

Variables
i,x,items
Variables
i,x,items
Extra Keywords
indices traverse traversal
Extra Keywords
indices traverse traversal
Imports
#include <ranges>
Code
for (auto const [i, x] : std::views::enumerate(items)) {
  std::cout << i << ": " << x << '\n';
}
Comments bubble
Depends on C++ 23
Doc URL
https://en.cppreference.com/w/cpp/ranges/enumerate_view