Is there a way in C++ to construct a float array initializing it's values?
For example, i do:
float* new_arr = new float[dimension];
for(unsigned int i = 0; i < dimension; ++i) new_arr[i] = 0;
Is it possible to do the assignment during the contruction?
Initializing a float Array in JavaArrays are declared with [] (square brackets). If you put [] (square brackets) after any variable of any type only that variable is of type array remaining variables in that declaration are not array variables those are normal variables of that type.
Yes. Of course by occupying more than one int16_t element of the array (two, as float has size 4).
Static arrays, and those declared directly in a namespace (outside any function), are always initialized. If no explicit initializer is specified, all the elements are default-initialized (with zeroes, for fundamental types).
dataType: This is the data type that specifies the type of elements to be stored in the array. It can be int, float, double, char.
In this particular case (all zeroes) you can use value initialization:
float* new_arr = new float[dimension]();
Instead of explicitly using new[]
you could use a std::vector<float>
instead:
std::vector<float> new_vec(dimension, 0);
float* new_arr = new float[dimension]();
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