I know for static arrays whose memory allocation is done on the stack are initialized by
int array[] = {1,2,3,4,5}
. I want to know how do you initialize a dynamically allocated array in C++.
The following method seems wrong.
int *array = new int[5]; array[] = {1,2,3,4,5};
Asked
Active
Viewed 27 times
1
-
1Another duplicate: https://stackoverflow.com/questions/7124899/initializer-list-for-dynamic-arrays – alter_igel Mar 30 '20 at 15:08
-
1The best way to do this is to use a vector and initialize it. `vector
vec{1,2,3,4,5}` – Arun Suryan Apr 02 '20 at 07:41