简体   繁体   English

如何生成一个以变量为大小的数组?

[英]How to generate an Array with a variable as size?

I'm trying to pass an array as a parameter using this function.我正在尝试使用此函数将数组作为参数传递。 In the function, I let the user enter how big the array is, as well as the values in said array.在函数中,我让用户输入数组的大小,以及所述数组中的值。 What I can't figure out is how to declare the variables in main that will allow me to use the function in main, and more specifically, how do I declare the array variable in main without knowing the size beforehand (user enters size in function).我想不通的是如何在 main 中声明变量以允许我在 main 中使用该函数,更具体地说,我如何在不知道大小的情况下在 main 中声明数组变量(用户在函数中输入大小) )。

void arrayFunction(int array1[], int arraySize);
    
int main() {
    
    int arrayLength;
    int arrayMain[];
    cout << "Enter length of array: " << endl;
    cin >> arrayLength;
        
    arrayFunction(arrayMain, arrayLength);
    
    return 0;
}

void arrayFunction(int array1[], int arraySize)
{
    cout << "Enter length of array: " << endl;
    cin >> arraySize;
    
    for(int i = 0; i < arraySize; i++)
    {
        cout << "Enter value #" << i + 1 << endl;
        cin >> array1[i];
    }
}

As the comments say, your best bet is to use a std::vector<int> instead:正如评论所说,最好的办法是使用std::vector<int>代替:

#include <vector>

std::vector<int> arrayFunction(){
    int arraySize;
    cout << "Enter length of array: " << endl;
    cin >> arraySize;

    std::vector<int> result(arraySize);

    for(int i = 0; i < arraySize; i++)
    {
        cout << "Enter value #" << (i + 1) << endl;
        cin >> result[i];
    }
    return result;
}

If you don't have access to std::vector , you'll need to build something similar use dynamic allocated arrays:如果您无权访问std::vector ,则需要构建类似使用动态分配数组的内容:

int* arrayFunction() {
    ...
    // instead of the std::vector<int> line, you can just do this:
    int* result = new int[arraySize];
    ...
}

but you'll need to make sure to call delete [] array;但你需要确保调用delete [] array; when you're done with the memory.当你完成记忆时。 std::vector will do this automatically so it's generally a safer approach to dynamic arrays. std::vector将自动执行此操作,因此它通常是动态数组的更安全方法。

It would be great to use vector.使用矢量会很棒。 But you can also use a traditional pointer to achieve this so-called dynamic array.但是你也可以使用传统的指针来实现这个所谓的动态数组。 This array size can be then specified during runtime instead of compile time.然后可以在运行时而不是编译时指定此数组大小。

Just modified a little from your code:只是从你的代码中修改了一点:

#include<iostream>
using namespace std;

void arrayGenerator(int* , int );
void arrayPrinter(const int* pArray, int arraySize);

int main() {
    int arrayLength;
    cout << "Enter length of array: " << endl;
    cin >> arrayLength;
    int* pArray = new int[arrayLength];
    arrayGenerator(pArray, arrayLength);
    arrayPrinter(pArray, arrayLength);
    delete [] pArray;
    return 0;
}
void arrayGenerator(int* pArray, int arraySize) {
    for(int i = 0; i < arraySize; i++)
    {
        cout << "Enter value #" << i + 1 << endl;
        cin >> pArray[i];
    }
}

void arrayPrinter(int* pArray, int arraySize) {
    cout << "This is the given array: \n";
    for (int i = 0; i < arraySize; i++)
        cout << i << " ";
    cout << endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM