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

3

我对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);
}

4
问题是什么? - Maroun
如果你想得到帮助,你需要提供Array::Insert的实现。 - Kevin
@JamesMcLaughlin 它是 C++11 中的新内容 http://www.cplusplus.com/reference/array/array/ - stonemetal
1
@stonemetal 这里使用的是 std::array,而不是 Array - bames53
啊,我不应该删除我的评论。那么这个神秘的“Array”是什么? - James M
显示剩余2条评论
3个回答

5

您正在传递数组的一个副本,因此任何更改都不会影响原始数组。请改为通过引用传递:

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

这并不是他唯一的问题。 - David G
@0x499602D2 能详细说明一下吗? - trojanfoe
他声明了一个对象,但是他却把它当作数组使用:Array<int> intarray(10)。应该改为 Array<int> intarray[10] - David G
2
@0x499602D2 我不确定那是正确的;构造函数可能是 Array::Array(size_t size),这似乎是合理的。但如果没有看到 Array.h,我不能确定。 - trojanfoe
谢谢,那解决了我遇到的问题。我也意识到我忘记声明函数了。现在看起来它运行得很好。 - Liam

2

这是一个更一般的有关参数传递的问题的特殊情况。

您可能需要考虑以下准则:

  1. If you want to pass something to a function to modify it inside the function (and make the changes visible to the caller), pass by reference (&).

    e.g.

    // '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. If you want to pass something that is cheap to copy (e.g. an int, a double, etc.) to a function to observe it inside the function, you can simply pass by value.

    e.g.

    // '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. If you want to pass something that is not cheap to copy (e.g. a std::string, std::vector, etc.) to a function to observe it inside the function (without modifying it), you can pass by const reference (const &).

    e.g.

    // '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. In modern C++11/14 there is also an additional rule (related to move semantics): if you want to pass something that is cheap to move and make a local copy of it, then pass by value and std::move from the value.

    e.g.

    // '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)

-1

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

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++;
    }
}

这对我来说至少有效,希望能对你有所帮助


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接