Question

Is there any way to use an anonymous array in a ranged for loop? I'm trying to do something like this

for(auto i: {1,2,3,5,7,11,17}) //do something
Était-ce utile?

La solution

Yes there is; it looks like this:

#include <initializer_list>  //MUST INCLUDE THIS!

for(auto i: {1,2,3,5,7,11,17}) //do something

Autres conseils

You may have trouble because there is no type ... instead of auto declare "i" as integer.

regards

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top