how-to initialize 'const std::vector' like a c array

后端 未结 10 769
走了就别回头了
走了就别回头了 2020-11-27 02:59

Is there an elegant way to create and initialize a const std::vector like const T a[] = { ... } to a fixed (and small) number of val

10条回答
  •  -上瘾入骨i
    2020-11-27 03:19

    How about:

    int ar[]={1,2,3,4,5,6};
    const int TotalItems = sizeof(ar)/sizeof(ar[0]);
    std::vector v(ar, ar+TotalItems);
    

提交回复
热议问题