Initialize float array when building

Is there a way in C ++ to build a float array initializing its 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 complete the task during creation?

+8
c ++
source share
2 answers
 float* new_arr = new float[dimension](); 
+18
source share

In this particular case (all zeros) you can use value initialization:

 float* new_arr = new float[dimension](); 

Instead of explicitly using new[] instead of std::vector<float> you can use

 std::vector<float> new_vec(dimension, 0); 
+8
source share

All Articles