When creating an array of fundamental type such as :
double *aPtr = new double[10];
Is there a way of initializing it to a specific value? ((ex)-1)
Is creating a new class the only way?
The fill_n function can be what you are looking for. It will assign the n first values of your array with a specific value.
double *aPtr = new double[10];
std::fill_n(aPtr, 10, -1); // Fill the aPtr array with the value -1 for the 10 first values.
I do not know a specific way to fill it directly with one same value at declaration.
you can use std::fill_n:
std::fill_n(aPtr , 10, -1);
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