-1

我有这个多维动态数组,我可以将它打印为六行六列零,但我不能将它返回原样。该函数应该初始化一个可以传递给其他函数的数组。我在这里做错了什么?我注释掉了“cout”部分,因为那不是我需要做的。谢谢!

long initializeArray (void)
{
    int i = 0, j = 0;
    typedef int* rollarray;
    rollarray *m = new rollarray[6];
    for (int i = 0; i < 6; i++) m[i] = new int[6];
    while (j < 6)
    {
        while (i < 6)
        {
            m[i][j] = 0;
            //cout << m[i][j] << " ";
            i++;
        }
        //cout << endl;
        i = 0;
        j++;
    }
}
4

4 回答 4

4

你应该声明你的函数:

int** initializeArray(void)

接着:

return m;

最后,如果你必须这样做。不要忘记您必须手动告诉使用此指针的其他任何东西它是一个 6 x 6 数组,并且delete[]当您完成所有这些数组时不要忘记它们。

于 2013-07-18T00:36:14.070 回答
2

关于为什么动态二维矩阵(这显然是一个)不是 C++ 的方式,我不会重复我的完整答案。

回答:一维或二维阵列,哪个更快?(开始阅读@长答案,或者为什么动态二维数据存储(指针到指针或向量的向量)对于简单/小型矩阵是“坏的”。

你会找到:

  • 一个相当详细的解释为什么你不想使用指针到指针的动态数组
  • 简单矩阵对象的示例类

您甚至不需要将数据初始化为零的函数。写吧

matrices::simple<int> matrix_object(6, 6);

得到一个大小为 6x6 的零初始化矩阵。

现在您可以通过以下方式访问元素

matrix_object(0,1) = 2; // sets 2nd element of first row to 2

将矩阵写入流的“C++ 方式”将涉及operator<<为该类定义,例如:

template<typename T>
std::ostream & operator<< (std::ostream &stream, matrices::simple<T> const & matrix)
{
  typedef typename matrices::simple<T>::size_type size_type;
  for (size_type i(0u); i<matrix.rows(); ++i)
  {
    for (size_type j(0u); j<matrix.cols(); ++j) 
         stream << std::setw(4) << std::right << matrix(i,j);
    stream << std::endl;
  }
  return stream;
}

您可以通过以下方式轻松输出矩阵:

std::cout << matrix_object << std::endl;

连同前面的片段,这将输出:

   0 2 0 0 0 0
   0 0 0 0 0 0
   0 0 0 0 0 0
   0 0 0 0 0 0
   0 0 0 0 0 0
   0 0 0 0 0 0

如果您想继续使用指针,则必须解决代码中的几个问题。我添加了两个参数来启用其他大小,但如果需要,可以再次将其替换为 6。

int** new_initialized_array (size_t const rows, size_t const cols)
{
    typedef int* rollarray;
    rollarray *m = new rollarray[rows];
    size_t allocated_arrays(0u);
    try
    {
      for (size_t i(0u); i < rows; ++i) 
      {
        m[i] = new int[cols];
        ++allocated_arrays;
        for (size_t j(0u); j<cols; ++j) m[i][j] = 0;
      }
    }
    catch (std::bad_alloc & e)
    {
      for (size_t i(0u); i < allocated_arrays; ++i) delete[] m[i];
      delete[] m;
      throw;
    }
    return m;
}

我解决的问题:

  1. 要返回指针,函数必须具有实际上是指针的返回类型(long 是无符号值)。
  2. 您需要跟踪您的分配。如果其中一个失败,您需要回滚其余的以避免内存泄漏。
  3. 你不需要双while循环。您的外部循环已经存在(您在其中分配的那个),因此您只需要一个内部循环来设置数组的初始值。
  4. 最后但并非最不重要的一点是,我重命名了该函数。它实际上并没有“初始化”现有数组,而是创建一个新的、已初始化的数组。

我只能建议阅读上面的链接(或有关如何根据 RAII 处理 2D 数据的任何其他资源)。

于 2013-07-18T00:36:47.070 回答
2

C++ :)

#include <vector>
#include <iostream>

std::vector<std::vector<int> > initializeVector() {
    std::vector<std::vector<int> > vec(6, std::vector<int>(6));

    int i = 0, j = 0;
    while (i < 6) {
        while (j < 6) {
            vec[i][j] = i+j;
            j++;
        }

        j = 0;
        i++;
    }

    return vec;
}

int main(int argc, char* argv[]) {
    std::vector<std::vector<int> > g_vec = initializeVector();

    int i = 0, j = 0;
    while (i < 6) {
        while (j < 6) {
            std::cout << g_vec[i][j] << std::endl;
            j++;
        }

        std::cout << "-----------------" << std::endl;
        j = 0;
        i++;
    }

    return 0;
}
于 2013-07-18T00:39:31.613 回答
1

是家庭作业吗?或者你想用 C++ 做矩阵代数?

如果它不是家庭作业,那么首先检查是否存在一些更易于使用且性能更高的东西。Pixelchemist 提出了很好的观点,因此您应该能够使用其他人的代码为您完成工作。

查看 Eigen 库:http ://eigen.tuxfamily.org/dox/TutorialAdvancedInitialization.html

// Initializing a 6x6 dynamic size matrix with zeros, adapted from documentation
using namespace Eigen;
ArrayXXf a3 = ArrayXXf::Zero(6, 6);
std::cout << a3 << "\n";
于 2013-07-18T00:41:40.917 回答