C++将数组传递到函数中

C++ Passing array into function

本文关键字:函数 数组 C++      更新时间:2023-10-16

我对C++还比较陌生,很难将数组传递到一个单独的函数中。很抱歉,我再次提出了一个问题,毫无疑问,这个问题以前已经回答了十几次,但我找不到任何与我的代码问题类似的问题。

int main()
{
    Array<int> intarray(10);
    int grow_size = 0;
    intarray[0] = 42;
    intarray[1] = 12;
    intarray[9] = 88;
    intarray.Resize(intarray.Size()+2);
    intarray.Insert(10, 6);
    addToArray(intarray);
    int i = intarray[0];
    for (i=0;i<intarray.Size();i++) 
    cout<<i<<'t'<<intarray[i]<<endl;
    Sleep(5000);
}
void addToArray(Array<int> intarray)
{
    int newValue;
    int newIndex;
    cout<<"What do you want to add to the array?"<<endl;
    cin >> newValue;
    cout<<"At what point should this value be added?"<<endl;
    cin >> newIndex;
    intarray.Insert(newValue, newIndex);
}

您正在传递数组的副本,因此任何更改都不会影响原始数组。通过参考:

void addToArray(Array<int> &intarray)
//                         ^

这是关于传递的参数的更一般问题的一个特例。

您可能需要考虑以下指南:

  1. 如果您想将某些内容传递给函数,以便在函数内修改(并使更改对调用方可见),则通过引用传递。(&)。

    例如

    // 'a' and 'b' are modified inside function's body,
    // and the modifications should be visible to the caller.
    //
    //     ---> Pass 'a' and 'b' by reference (&) 
    //
    void Swap(int& a, int& b)
    {
        int temp = a;
        a = b;
        b = temp;
    }
    
  2. 如果您想将复制成本较低的东西(例如intdouble等)传递给函数,以便在函数内观察,您可以简单地按值传递

    例如

    // 'side' is an input parameter, "observed" by the function.
    // Moreover, it's cheap to copy, so pass by value. 
    //
    inline double AreaOfSquare(double side)
    {
        return side*side;
    }
    
  3. 如果您想将复制不便宜的东西(例如std::stringstd::vector等)传递给函数,以便在函数内观察(无需修改),则可以通过常量引用传递(const &)。

    例如

    // 'data' is an input parameter, "observed" by the function.
    // It is in general not cheap to copy (the vector can store
    // hundreds or thousands of values), so pass by const reference.
    //
    double AverageOfValues(const std::vector<double> & data)
    {
        if (data.empty())
            throw std::invalid_argument("Data vector is empty.");
        double sum = data[0];
        for (size_t i = 1; i < data.size(); ++i)
            sum += data[i];
        return sum / data.size();
    }
    
  4. 在现代C++11/14中,还有一条额外的规则(与移动语义有关):如果你想传递便宜的东西并对其进行本地复制,那么通过值和值中的std::move传递。

    例如

    // 'std::vector' is cheap to move, and the function needs a local copy of it.
    // So: pass by value, and std::move from the value.
    //
    std::vector<double> Negate(std::vector<double> v)
    {
        std::vector<double> result( std::move(v) );
        for (auto & x : result)
            x *= -1;
        return result;
    }
    

由于在addToArray()函数中,您修改了Array<int>参数,并且您希望修改对调用者可见,因此可以应用规则#1,并且通过引用传递&):

void addToArray(Array<int> & intarray)

我的建议是使用指针,像往常一样声明函数

void yourFunction(int * arrayPointer);

main()功能中,您应该输入

int yourArray[8] = {11, -2, 45, 37, 18, 35};
int * arrayPointer = &yourFunction[0];
yourFunction(yourArray); // call your function

最后在yourFunction()

void yourFunction(int * arrayPointer)
{
    // print all number in the array
    for(int x = 0; x < 6; x++)
    {
        cout << *arrayPointer << " ";
        arrayPointer++;
    }
}

这至少对我有效,希望这将有助于