在沒有全局變量的C ++函數中創建數組

[英]Create an array in a function in C++ without a global variable


So I would like to create an array in a function with the size set by a number coming in as the parameter. Here is an example:

所以我想在函數中創建一個數組,其大小由作為參數的數字設置。這是一個例子:

void temp_arr ( const int array_size ) {
     int temp_arr[array_size]; //ERROR array_size needs to be a constant value
    //Then do something with the temp arr
}

Even if the parameter is a const int, it will not work. I would like to not use a global const and not use vectors. I am just curious as I am learning C++. I would like for it to make it so that the array size is different each time the function is called. Is there a solution to this or am I to create a const variable and the array before the function is called?

即使參數是const int,它也不起作用。我想不使用全局const而不使用向量。我很好奇,因為我正在學習C ++。我希望它能夠使每次調用函數時數組大小不同。是否有解決方案或我是否在調用函數之前創建一個const變量和數組?

5 个解决方案

#1


7  

Using a template function:

使用模板功能:

template<std::size_t array_size>
void temp_arr()
{
    int temp_arr[ array_size ];
    // ...work with temp_arr...
}

You can then call the function with this syntax:

然后,您可以使用以下語法調用該函數:

temp_arr<32>(); // function will work with a 32 int statically allocated array

Note

Every call with a different value of array_size will instantiate a new function.

每個具有不同array_size值的調用都將實例化一個新函數。

#2


2  

When you pass a value in this function, the value is not a constant. Defining an array must be done with a constant value. Although you have used const int array_size, that only creates an integer that is constant within the function. So in a way, if you pass a variable value in the function, it takes it as a variable. Thus it produces an error. Yes, you are to create a constant and pass it during the function call.

在此函數中傳遞值時,該值不是常量。必須使用常量值來定義數組。雖然您已經使用了const int array_size,但它只創建了一個在函數內是常量的整數。所以在某種程度上,如果在函數中傳遞變量值,它會將其作為變量。因此它產生錯誤。是的,您要創建一個常量並在函數調用期間傳遞它。

#3


0  

If you don't have issue with memory let me tell you an easy way:-

如果您沒有內存問題,請告訴您一個簡單的方法: -

   void temp_arr ( const int array_size )
   {
       //lets just say you want to get the values from user and range will also be decided by the user by the variable array_size

       int arr[100];   //lets just make an array of size 100 you can increase if according to your need;
       for(int i=0; i<array_size ; i++)
       {
         scanf("%d",&arr[i]);
       }
   }

I know this is not a perfect solution but just a easy way for beginners.

我知道這不是一個完美的解決方案,但對初學者來說只是一個簡單的方法。

#4


0  

You can use:

您可以使用:

int const size = 10;
int array[size]; 

to create an array in C++. However, you cannot use

用C ++創建一個數組。但是,你不能使用

void temp_arr ( const int array_size ) {
     int temp_arr[array_size];
}

to create an array unless the compiler supports VLAs as an extension. The standard does not support VLAs.

除非編譯器支持VLA作為擴展,否則創建一個數組。該標准不支持VLA。

The const qualifier in an argument type merely makes the variable const in the function -- you can't modify its value. However, the value cannot necessarily be determined at compile time.

參數類型中的const限定符僅在函數中使用變量const - 您無法修改其值。但是,該值不一定在編譯時確定。

For example, you can call the function using:

例如,您可以使用以下命令調用該函數:

int size;
std::cout << "Enter the size of the array: ";
std::cin >> size;
temp_arr(size);

Since the value cannot be necessarily be determined at compile time, it can't be used to create an array.

由於該值不一定在編譯時確定,因此不能用於創建數組。

#5


0  

You could use a std::unique_ptr:

你可以使用std :: unique_ptr:

void temp_arr(int array_size)
{
    auto temp_arr = std::make_unique<int[]>(array_size);
    // code using temp_arr like a C-array
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/07/10/720d60afd820d2821ef2ee79ac8ae7aa.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com