c++如何将两个数组传递给函数并同时返回它们

c++ how to pass two arrays to a function and return them both?

本文关键字:函数 返回 数组 两个 c++      更新时间:2023-10-16

我有两个数组:

      int sudoku[9][9];
      bool possiblevalues[81][9];

我想初始化它们。我将它们传递给函数initialize (sudoku, possiblevalues)。这个函数初始化数组并返回它们。现在我的问题是:我怎样才能正确地退货?因为它不允许我使用指针或引用。

通过引用获取数组,那么您不需要返回它们:

void initialize(int (&sudoku)[9][9], bool (&possiblevalues)[81][9])
{
    // code to initialize here.
    // Any changes you make here will be reflected to the arrays
    // that have been passed to the function
}

像这样使用原始数组并不是特别"好"的c++。我建议你学习如何使用std::vectorstd::array