C++ creating arrays

后端 未结 7 1107
一个人的身影
一个人的身影 2021-01-11 17:49

Why can\'t I do something like this:

int size = menu.size;
int list[size];

Is there anyway around this instead of using a vector? (arrays a

7条回答
  •  借酒劲吻你
    2021-01-11 18:33

    AFAIK In C++03, there are no variable-length-arrays (VLA):

    you probably want to do this:

    const int size = menu.size;
    int list[size]; // size is compile-time constant
    

    or

    int *list = new int[size]; // creates an array at runtime;
    delete[] list;             // size can be non-const
    

提交回复
热议问题