mardi 1 novembre 2016

Cycles in c++ like in python (range-based for) [duplicate]

This question already has an answer here:

What is the easiest way to do cycles in c ++ like in python?

for i in range(10): #or range(4, 10, 2) etc
    foo(i)

I mean something simple and one-line like this

for(auto i: range(10)) //or range(4, 10, 2) or range(0.5, 1.0, 0.1) etc
    foo(i);

but not like this:

std::vector<int> v(10);
std::iota(begin(v), end(v), 0);
for(auto i: v) {
    foo(i);
}

Or this

for(auto i: []{vector<size_t> v(10); return iota(begin(v), end(v), 0), v;}() ) {
    foo(i);         
}

Of course, it is not difficult to use these examples or just for(;;) but I hope there is a way to do it briefly and succinctly in python.

Aucun commentaire:

Enregistrer un commentaire