array

Sep 1, 2014 at 7:12am
which is more readable?

int anArray[]={1,2};

or

int anArray[2]={1,2};
Sep 1, 2014 at 7:19am
I prefer something like
1
2
std::size_t const size = 2;
int anArray[size] = {1, 2};
Though, I suppose something like this would be preference.
Sep 1, 2014 at 7:22am
whats the use of size_t?
Sep 1, 2014 at 7:26am
It's an unsigned integer. That is used a lot in the standard such as the size of a container. I suppose I could have done unsigned const size = 2; or even something like std::uint32_t const size = 2;

Though this will compile as well int const size = 2; I jsut prefer to make it unsigned if it is positive.
Sep 1, 2014 at 8:48am
which is more readable?

They are different.
In the first the size of the array is implicitly taken from the initializer list. One has to count the members of the list.

The second, however decouples array size from the list size. This is legal:
1
2
std::size_t const size = 42;
int anArray[size] = {1, 2};

You still have to count the list in order to know how all elements of the array are initialized. The big bonus here is that the constant 'size' is available when the array is used.
Sep 1, 2014 at 9:02am
okay thank you
Topic archived. No new replies allowed.