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

后端 未结 29 3230
终归单人心
终归单人心 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:41

    For vector initialisation -

    vector v = {10,20,30}
    

    can be done if you have C++11 compiler.

    Else, you can have an array of the data and then use a for loop.

    int array[] = {10,20,30}
    for(unsigned int i=0; i

    Apart from these, there are various other ways described above using some code. In my opinion, these ways are easy to remember and quick to write.

提交回复
热议问题