C++ Version of Python-loop "for i, num in enumerate(list):"

Viewed 802

In python, you can iterate a list like below. Is there a similarly short way of doing this in C++?

list = [1,2,3,4,5]
for i, num in enumerate(list):
     # do stuff

Something like for(int num : list) is close, but not the same.

1 Answers

C++ 17 time!

for(auto [it, i] = tuple{list.begin(), 0}; it != list.end(); it++, i++)
{
   cout << *it; //actual item
   cout << i; //index value
}
Related