+ 1
C++ iterator and single instruction
Did you ever note, in C++, if you use an iterator in a for cycle you can put a single instruction and, if you try and put multiple instructions with curly braces, you get a mysteryous compilation error?
5 ответов
+ 3
As far as I know, you can have multiple expressions inside a range-based for, collected inside curly braces.
Perhaps the error was regarding the use of the loop variable as an iterator? Because the loop variable is a normal variable assigned with the value of the elements in the range one by one. It is not an iterator. The iterators of the range are accessed internally.
If possible, kindly share an example of the code fragment which results in a compile error.
+ 3
Paolo, I'm sorry, I didn't fully understand where in that code I was supposed to put additional instructions. When you wrote "after one of the two for iterators ..."; did you mean between the enhanced for loop and the `if(bGuitar)` block? or inside the loop body? I'm confused because as I understand it, an enhanced for loop cannot have more than one iterator (in your code `it` was an iterator, internally handled).
If you meant between the enhanced for loop and the `if(bGuitar)` block then I suppose there could possibly be a problem, considering the `if(bGuitar)` block inside the enhanced for loop body wasn't secured by curly brackets, additional instructions above the `if(bGuitar)` block will not be a part of the loop body, I assume you know that already, so I'd like to hear more from you.
+ 2
Paolo can you please share your code? I'm not getting an idea how that can cause a problem : )
+ 1
🤔 can you break it down for me because what i am thinking isn't a compilation error
+ 1
In this code:
https://code.sololearn.com/ctOJeeR0ZBON/?ref=app
try and use a couple of instructions after one of the two for iterators, putting instructions in a curly braces block