简体   繁体   English

C ++如何从函数返回多个不同类型的数组

[英]C++ How to return multiple arrays of different types from a function

I am trying to write a function that accepts 3 different arrays. 我正在尝试编写一个接受3个不同数组的函数。 These arrays are of type string, double, and double, respectively. 这些数组的类型分别为string,double和double。 The function will fill these arrays with data and then return them to main. 函数将用数据填充这些数组,然后将它们返回到main。 However, I am unsure what to declare as the return type for the function, as all of the arrays do not hold the same data types. 但是,我不确定要声明什么作为函数的返回类型,因为所有数组都不具有相同的数据类型。 The function that will accept the arrays as arguments is listed below 下面列出了将数组作为参数接受的函数

void additems(string arry1[], double arry2[], double arry3[], int index)
{
/*************************  additems **************************
NAME: additems
PURPOSE: Prompt user for airport id, elevation, runway length.  Validate input and add to 3 seperate parallel arrays
CALLED BY: main
INPUT: airportID[], elevation[], runlength[], SIZE
OUTPUT: airporID[], elevation[], runlength[]
****************************************************************************/
    //This function will prompt the user for airport id, elevation, and runway     length and add them to 
    //separate parallel arrays
    for (int i=0; i<index; i++)
    {
        cout << "Enter the airport code for airport " << i+1 << ". ";
        cin >> arry1[i];
        cout << "Enter the maximum elevation airport " << i+1 << " flys at (in ft). ";
        cin >> arry2[i];
        while (arry2[i] <= 0)
        {
            cout << "\t\t-----ERROR-----";
            cout << "\n\t\tElevation must be greater than 0";
            cout << "\n\t\tPlease re enter the max elevation (ft). ";
            cin >> arry2[i];
        } 
        cout << "Enter the longest runway at the airport " << i+1 << " (in ft). ";
        cin >> arry3[i];
        while (arry3[i] <= 0)
        {
            cout << "\t\t-----ERROR-----";
            cout << "\n\t\tRunway length must be greater than 0";
            cout << "\n\t\tPlease re enter the longest runway length (ft). ";
            cin >> arry3[i];
        }
        cout << endl;
    }   

    return arry1, arry2, arry3;
   }

Thanks in advance for considering my question 预先感谢您考虑我的问题

You do not need to return the arrays, as they are modified by the function. 您不需要返回数组,因为它们是由函数修改的。 When you pass arrays to a function like this, the array is passed by reference. 当您将数组传递给类似这样的函数时,该数组将通过引用传递。 Normally a data type is passed by value ( ie copied), but arrays are treated a little like pointers. 通常,数据类型通过值传递( 复制),但是将数组视为类似于指针。

So just return void , or if you like you could return some kind of value to indicate success (if that's appropriate). 因此,只要返回void ,或者如果您愿意,就可以返回某种值来表示成功(如果合适)。 You may want to return an integer to say how many records were entered (if the user has the option to enter less than index records). 您可能需要返回一个整数,以说明输入了多少条记录(如果用户可以选择输入少于index记录的选项)。

You can just say 你可以说

return;

or leave it out altogether. 或完全省略。 You're modifying the passed-in arrays in-place, so there's no need to return anything. 您正在就地修改传入的数组,因此无需返回任何内容。

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

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