Is there a way to statically-initialize a dynamically-allocated array in C++?

前端 未结 6 852
囚心锁ツ
囚心锁ツ 2020-12-03 17:40

In C++, I can statically initialize an array, e.g.:

int a[] = { 1, 2, 3 };

Is there an easy way to initialize a dynamically-allocated array

6条回答
  •  悲&欢浪女
    2020-12-03 18:16

    Never heard of such thing possible, that would be nice to have.

    Keep in mind that by initializing the array in the code that way

    int a[] = { 1, 2, 3 };
    

    ..... only gains you easier code writing and NOT performance. After all, the CPU will do the work of assigning values to the array, either way you do it.

提交回复
热议问题