【问题标题】:c++ how to pass two arrays to a function and return them both?c++ 如何将两个数组传递给一个函数并将它们都返回?
【发布时间】:2013-05-03 14:20:06
【问题描述】:

我有两个数组:

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

我想同时初始化它们。我将它们传递给函数初始化(数独,可能值)。这个初始化数组并返回它们。现在是我的问题:如何正确退回它们?因为它不允许我使用指针或引用。

【问题讨论】:

  • 嗯... wut?不让你使用指针?
  • 您有编译问题吗?
  • 使用向量代替,因为它是 c++
  • 如果你不能使用指针或引用,你将不得不使用不同的函数。您不能在一个函数中返回两个不同的数组。
  • 使用std::vector<std::vector<int> > & std::vector<std::vector<bool> >

标签: c++ arrays return sudoku solver


【解决方案1】:

通过引用获取数组,则无需返回它们:

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

【讨论】:

    猜你喜欢
    • 2015-06-04
    • 2015-08-07
    • 1970-01-01
    • 2016-10-02
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 2022-10-12
    相关资源
    最近更新 更多