I usually define this, although I was told that I would go to hell:
#define forsn(i, s, n) for(int i = (s); i < (n); ++i) #define forn(i, n) forsn(i, 0, n) #define forall(it, g) for(typeof g.begin() it = g.begin(); it != g.end(); ++it)
Then, to iterate over from 0 to n the general problem, I will say forn(i, n) foo(i); and loop any standard container c, say forall(it, c) foo(it); Note that typeof is a GCC extension for the standard.
source share