C++ creating arrays

后端 未结 7 1157
一个人的身影
一个人的身影 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:26

    C++ does not allow variable length arrays. The size must be known at compile-time. So you can't do that.

    You can either use vectors or new.

    vector list;
    

    or

    int *list = new int[size];
    

    If you go with the latter, you need to free it later on:

    delete[] list;
    

    arrays are faster, so I wanted to use arrays

    This is not true. Where did you hear this from?

提交回复
热议问题