如何从用户定义的函数返回3D数组? [英] How to return a 3D array from user defined function?
问题描述
我想将3维数组从用户定义的函数返回到我的 main
函数中.
I want to return a 3-dimensional array from a user-defined function into my main
function.
让我们考虑一个简单示例,说明 a
, b
和 c
数组的任何特定值;
Let's consider a simple example for any particular values of a
, b
and c
arrays;
int*** my_function(int a[], int b[], int c[])
{
for (int i = 0; i < 2; i++)
{
for (int j; j < 2; j++)
{
for (int k; k < 2; k++)
{
array[i][j][k] = a[i] * b[j] * c[k];
}
}
}
return array
}
推荐答案
使用指针的更好替代方法是使用 std :: vector
,因为它将处理内存分配和释放的细节.
A better alternative to using pointers is to use std::vector
as that will take care of the details of memory allocation and deallocation.
如何初始化3D矢量?
3D向量只是一个在其中包含两个其他向量的向量.因此,如果要使用值A初始化大小为X * Y * Z的3D向量,则可以通过以下方式完成:
3D vector is simply a vector containing two other vectors inside it. So, if you want to initialize a 3D vector of size X * Y * Z with a value A. Then it can be done as:
vector<vector<vector<int> > > vec (X,vector<vector<int> >(Y,vector <int>(Z,A)))
如何从函数返回它?
您可以从下面的示例中查看如何将其返回.
You can see how to return it from the below example.
vector<vector<vector<int> > > sample_function()
{
//sample code
return vec;
}
这篇关于如何从用户定义的函数返回3D数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!