Possible Duplicate:
Take the address of a one-past-the-end array element via subscript: legal by the C++ Standard or not?
In the C++ standard library, many algorithms takes the begin()
and end()
iterator as argument. But the trick is that the end()
iterator is after the end of the container. If I want to apply algorithms to standard c-array I have to pass the equivalent of begin()
and end()
pointers.
The question is the following :
const unsigned int size = 10;
int array[size];
std::iota(&array[0], &array[size], 0); // <- Version 1
std::iota(&array[0], &array[0]+size, 0); // <- Version 2
Are the two versions strictly equivalent ? Can I use version 1 without any problem according to the C++ standard ?
My doubt comes from the fact that &array[size]
access the element after the end of the array and then takes it address whereas &array[0]+size
do not access the element after the end of the array.
Just use std::begin
and std::end
from <iterator>
and don't worry - they'll do the right thing. :)