Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using an anonymous array in a ranged for loop in c++

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
like image 867
German Capuano Avatar asked Jun 02 '26 07:06

German Capuano


1 Answers

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
like image 173
Casey Avatar answered Jun 03 '26 20:06

Casey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!