Use new operator to initialise an array

后端 未结 2 1753
一生所求
一生所求 2021-02-02 08:34

I want to initialise an array in the format that uses commas to separate the elements surrounded in curly braces e.g:

int array[10]={1,2,3,4,5,6,7,8,9,10};
         


        
2条回答
  •  时光取名叫无心
    2021-02-02 09:36

    In the new Standard for C++ (C++11), you can do this:

    int* a = new int[10] { 1,2,3,4,5,6,7,8,9,10 };
    

    It's called an initializer list. But in previous versions of the standard that was not possible.

    The relevant online reference with further details (and very hard to read) is here. I also tried it using GCC and the --std=c++0x option and confirmed that it works indeed.

提交回复
热议问题