Tag Archive for range-based for loop

The C++0x Range-Based For Loop

C++0x adds a new looping structure: the Range-Based for loop. This makes it easier to loop over elements of lists. It works with standard C arrays and types that have begin() and end() functions returning iterators like almost all STL containers.

Below is an example of a range-based for loop looping over a standard C-style array, incrementing each value by 1.

int myArray[3] = {1, 2, 3};
for(auto& el : myArray) {
    ++el;
}

Note that this example also uses the auto type deduction introduced in C++0x.
Now it’s only waiting until the C++ compilers start supporting it 🙂

Share