C++11 Feature

Wow - thanks for covering Ranged based for loops! I’ve not coded in c++ for some time, and i’m clearly unfamiliar with anything new from C++ 11.

I always felt that for loops were needlessly wordy - i’m very pleased to see this new ability.

I’ve pasted in info from the wiki page on this below:

Range-based for loop
C++11 extends the syntax of the for statement to allow for easy iteration over a range of elements:

int my_array[5] = {1, 2, 3, 4, 5};
// double the value of each element in my_array:
for (int& x : my_array)
    x *= 2;

// similar but also using type inference for array elements
for (auto& x : my_array)
    x *= 2;
This form of for, called the “range-based for”, will iterate over each element in the list. It will work for C-style arrays, initializer lists, and any type that has begin() and end() functions defined for it that return iterators. All the standard library containers that have begin/end pairs will work with the range-based for statement.

From the wiki article on C++11

Privacy & Terms