C++ specify array indexes in initializer, like C

后端 未结 3 650
余生分开走
余生分开走 2021-01-17 12:18

I used C before (embedded stuff), and I can initialize my arrays like that:

int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };

i

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-17 12:42

    No, it is not possible to do it in C++ like that. But you can use std::fill algorithm to assign the values:

    int widths[101];
    fill(widths, widths+10, 1);
    fill(widths+10, widths+100, 2);
    fill(widths+100, widths+101, 3);
    

    It is not as elegant, but it works.

提交回复
热议问题