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

标签 c++ arrays

我正在尝试编写一个接受 3 个不同数组的函数。这些数组的类型分别为字符串、 double 和 double 。该函数将用数据填充这些数组,然后将它们返回给 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 << "\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;
   }

预先感谢您考虑我的问题

最佳答案

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

因此只需返回 void,或者如果您愿意,您可以返回某种类型的值来指示成功(如果合适的话)。您可能希望返回一个整数来表示输入了多少条记录(如果用户可以选择输入少于 index 条记录)。

关于C++ 如何从一个函数返回多个不同类型的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13061613/

相关文章:

c++ - 将 malloc 转换为 new 或 std::vector

c++ - 为什么我可以在不包含 STL 的情况下使用 nullptr?

c++ - ReadFile std::unique_ptr 对比 std::vector 对比 std::string

javascript - 将在线 CSV 文件(Google Spreadsheet)引入 JavaScript 数组

python - Numpy 比较不相等的行并使两个数组具有相同的维度

c - 返回类型为 IPlImage 数组

c++ - 有没有更好的方法来初始化 C++ 中分配的数组?

c++ - 如何生成不同对的不等数?

c++ - “no match for ' operator< '” 尝试插入到 std::set 时

javascript - 使用 JavaScript (Ember.js) 将 TMDb 中的流派 ID 与流派名称进行匹配