Simpler way to set multiple array slots to one value

前端 未结 10 2243
我寻月下人不归
我寻月下人不归 2021-02-18 16:31

I\'m coding in C++, and I have the following code:

int array[30];
array[9] = 1;
array[5] = 1;
array[14] = 1;

array[8] = 2;
array[15] = 2;
array[23] = 2;
array[1         


        
10条回答
  •  醉梦人生
    2021-02-18 16:53

    Any fancy trickery you do will be unrolled by the compiler/assembler into exactly what you have. Are you doing this for readability reasons? If your array is already init, you can do:

    array[8] = array[15] = array[23] = array[12] = 2;
    

    But I stress my point above; it will be transformed into exactly what you have.

提交回复
热议问题