I want to combine these three features together:
into one example using C++11.
std::for_each(arr, arr + sizeof(arr) / sizeof(int), [&](int x) { std::cout<<x<<" ";});
How to convert that code to operate over std::array ?
You want array::begin and array::end of the array, for the two first parameters of for_each(), which will mark the start and end of the array.
Then the third parameter is a function, in your case a lambda function, like this:
std::for_each(myarray.begin(), myarray.end(), [](int x) { std::cout << x <<" "; });
PS: For a more generic approach, you could use std::begin() and std::end(), so that if the container changes (from std::array to std::vector for example), you can keep this code unchanged.
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