返回多维动态数组
Returning a multidimensional dynamic array?
我有一个多维动态数组,我可以将其打印为六行六列零,但我不能将其原样返回。该函数应该初始化一个可以传递给其他函数的数组。我在这里做错了什么?我评论掉了"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++;
}
}
您应该声明您的函数:
int** initializeArray(void)
然后:
return m;
最后,如果你必须这样做的话。不要忘记,您必须手动告诉使用此指针的其他任何对象它是一个6 x 6数组,并且在使用完这些数组后,不要忘记delete[]
。
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;
}
我不会重复我的完整答案,解释为什么动态二维矩阵(这显然是一个)不是C++的发展方向。
回答:1D或2D阵列,什么更快?(开始阅读@长答案,或者为什么动态二维数据存储(指针到指针或矢量的矢量)对于简单/小矩阵来说"不好")
你会发现:
- 一个相当详细的解释为什么您不想使用指针到指针的动态数组
- 简单矩阵对象的示例类
您甚至不需要一个将数据初始化为零的函数。只需写入
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 00 0 0 00 0 0 00 0 0 00 0 0 00 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;
}
我解决的问题:
- 若要返回指针,函数的返回类型必须实际为指针(long是无符号值)
- 您需要跟踪您的分配情况。如果其中一个失败,则需要回滚其余部分以避免内存泄漏
- 你不需要双while循环。您已经有了外部循环(您在其中分配的那个),所以您只需要一个内部循环来设置数组的初始值
- 最后但并非最不重要的是,我重命名了函数。它实际上并没有"初始化"现有的数组,而是创建一个新的初始化数组
我只能建议阅读上面的链接(或任何其他关于如何根据RAII处理2D数据的资源)。
这是家庭作业吗?或者你正在尝试用C++做矩阵代数?
如果这不是家庭作业,那么首先检查是否存在对你来说更容易使用和更具表现力的东西。Pixelchemister提出了很好的观点,所以你应该能够使用别人的代码来为你工作。
看看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";
- std::向量与传递值的动态数组
- 将数组动态分配到具有指针参数的函数中
- 我可以以某种方式使我的矢量/数组动态更改装置数量吗?
- 使用 new: "potentially uninitialized pointer"将对象数组动态分配给指针
- 为什么C 没有方便的方式来为多维数组动态分配内存
- 如何制作具有动态大小的数组?动态数组的一般用法(可能还有指针)
- c++中的数组动态内存分配
- 多维数组动态分配的内存图
- 如何在 c++ 中使用 2-D 固定数组为 4-D 数组动态分配内存
- C++ 我在数组/动态内存方面遇到了一些问题
- C++ 中从数组动态强制转换
- C++中高维数组动态内存分配的正确方法.
- 从 C++ 结构中包含的指针数组动态分配和访问内存
- 对象数组动态内存分配
- 为二维数组动态分配内存时出错
- 如何将2d数组动态分配给结构
- 为结构数组c++动态分配内存
- 将数组动态添加到多维数组
- 类中的类数组-动态数组(c++)有问题
- 分配对象数组动态冻结