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

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

本文关键字:同类型 数组 返回 函数      更新时间:2023-10-16

我正在尝试编写一个接受3个不同数组的函数。这些数组分别为字符串,双重和双重。该功能将充满数据填充这些数组,然后将它们返回到MAIN。但是,我不确定该声明为函数的返回类型,因为所有数组都不容纳相同的数据类型。作为参数,将接受数组的功能在下面列出

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 << "tt-----ERROR-----";
            cout << "nttElevation must be greater than 0";
            cout << "nttPlease 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 << "tt-----ERROR-----";
            cout << "nttRunway length must be greater than 0";
            cout << "nttPlease re enter the longest runway length (ft). ";
            cin >> arry3[i];
        }
        cout << endl;
    }   
    return arry1, arry2, arry3;
   }

事先感谢您考虑我的问题

您不需要返回数组,因为它们是由函数修改的。当您将数组传递到这样的函数时,该数组将通过参考传递。通常,数据类型是按值传递的( ie 已复制),但是数组的处理方式有点像指针。

因此,只需返回void,或者如果您喜欢的话,您可以返回某种价值以表示成功(如果合适的话)。您可能需要返回整数以说出输入了多少个记录(如果用户可以选项输入小于index记录)。

您可以说

return;

或完全将其排除在外。您正在修改传递的阵列,因此无需返回任何内容。