What is the easiest way to initialize a std::vector with hardcoded elements?

后端 未结 29 3274
终归单人心
终归单人心 2020-11-22 05:07

I can create an array and initialize it like this:

int a[] = {10, 20, 30};

How do I create a std::vector and initialize it sim

29条回答
  •  我在风中等你
    2020-11-22 05:45

    If you can, use the modern C++[11,14,17,...] way:

    std::vector vec = {10,20,30};
    

    The old way of looping over a variable-length array or using sizeof() is truly terrible on the eyes and completely unnecessary in terms of mental overhead. Yuck.

提交回复
热议问题