簡體   English   中英

C ++如何從函數返回多個不同類型的數組

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

我正在嘗試編寫一個接受3個不同數組的函數。 這些數組的類型分別為string,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記錄的選項)。

你可以說

return;

或完全省略。 您正在就地修改傳入的數組,因此無需返回任何內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM