Question

I understand how to use a for loop like

for (x = 0 ; x<5 ; x++ ) { // some_code }

in C++ but what about a for loop like

for x in y:

which exist at least in python

Was it helpful?

Solution

The closest equivalent is a range based for-loop. For example,

auto y = {0, 1, 2, 3, 4, 5, 6};

for (auto i : y)
{
  // do something with i
}

There are more details, but these depend on what you want to do. The C++ semantics are quite different to python's.

OTHER TIPS

In C++ you can use std::for_each which can be answer for you question. More information you can find here.

The for i in range(5) loop would "look" for i in the [0, 1, 2, 3, 4], so it would be quite similar in working as a C++ for (int i = 0; i < 5; ++i).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top