I would like to iterate in C++ over a set of values. In python, it looks like
for v in [v1, v2, v3]:
do_something()
What is the correct way to do it in C++?
for i in range(0,len(argv)): arg = argv[i] if arg == '--flag1': opt1 = argv[i+1] i+=1 continue if arg == '--anotherFlag': optX = argv[i+1] optY = argv[i+2] optZ = argv[i+3] i+=3 continue ...
For loop allows a programmer to execute a sequence of statements several times, it abbreviates the code which helps to manage loop variables. While loop allows a programmer to repeat a single statement or a group of statements for the TRUE condition. It verifies the condition before executing the loop.
for (const auto& v : {v1, v2, v3}) { do_something(); }
Would be equivalent (except for the fact that the elements in the initializer list will conceptually be copied - even if the optimizer elides those copies - so they will need to be copyable).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With