How can I declare an array inside a function according to the size of a parameter?

后端 未结 3 1478
我在风中等你
我在风中等你 2021-01-13 07:51

Here is what I have tried:

int fun1(vector s)
{ 
    const int n = s.size();
    int arr[n]; //<----want to declare an array of length s.size()         


        
3条回答
  •  被撕碎了的回忆
    2021-01-13 08:35

    Declaring array by int arr[N]; the size N must be determined in compile-time (except for some compiler extensions which allow you to define them in run-time too). By the way, you can make it:

    std::unique_ptr arr (new int [n]);
    
    // ... or ...
    
    std::vector arr(n);
    

提交回复
热议问题