I am switching from Java to C++ and I was wondering whether C++ contains the enhanced for loops that I used in java, in example:
int[] numbers = {1,2,3,4,5,6,7,8,9,10};
for (int item : numbers) {
System.out.println("Count is: " + item);
}
Is this same “shortcut” possible in C++?
In C++11, if your compiler supports it, yes it is. It’s called range-based for.
It works for C style arrays and any type that has functions
begin()andend()that return iterators. Example: