Is it possible to allow a user to enter an array size with a keyboard?

后端 未结 5 1927
萌比男神i
萌比男神i 2021-01-13 10:21

Is it possible to allow the user to enter the size of an array with a keyboard?

I know that arrays can\'t change size. The only solution I could think of is this:

5条回答
  •  半阙折子戏
    2021-01-13 11:08

    Variable Length Arrays are not approved by C++ standard. C++ Standard mandates that the size of an array must be an compile time constant.
    You can use VLA's through compiler extension provided in gcc but note the your code is not portable if you do so.

    Standard approved way of doing this is to use std::vector.

提交回复
热议问题