从函数返回二维数组 我是一个C++新手 我试图从函数返回2d数组。 是这样的 int **MakeGridOfCounts(int Grid[][6]) { int cGrid[6][6] = {{0, }, {0, }, {0, }, {0, }, {0, }, {0, }}; return cGrid; }
在代码段中,您正在(尝试做)/做的是从函数返回一个局部变量,这是完全不推荐的,根据标准,这也是不允许的从函数返回二维数组 我是一个C++新手 我试图从函数返回2d数组。 是这样的 int **MakeGridOfCounts(int Grid[][6]) { int cGrid[6][6] = {{0, }, {0, }, {0, }, {0, }, {0, }, {0, }}; return cGrid; },c++,multidimensional-array,C++,Multidimensional Array,在代码段中,您正在(尝试做)/做的是从函数返回一个局部变量,这是完全不推荐的,根据标准,这也是不允许的 如果你想从你的函数中创建一个int[6][6],你要么在免费存储中为它分配内存(即使用新的T/malloc或类似函数),要么将一个已经分配的内存块传递给MakeGridOfCounts,这样代码就不起作用了,如果我们修复它,它将不能帮助你学习正确的C++。如果你做些不同的事情会更好。原始数组(尤其是多维数组)很难在函数之间正确传递。我认为最好从一个表示数组但可以安全复制的对象开始。在文档中查找
如果你想从你的函数中创建一个
int[6][6]
,你要么在免费存储中为它分配内存(即使用新的T/malloc或类似函数),要么将一个已经分配的内存块传递给MakeGridOfCounts
,这样代码就不起作用了,如果我们修复它,它将不能帮助你学习正确的C++。如果你做些不同的事情会更好。原始数组(尤其是多维数组)很难在函数之间正确传递。我认为最好从一个表示数组但可以安全复制的对象开始。在文档中查找std::vector
在代码中,可以使用
向量
,也可以使用36个元素模拟二维数组向量
此代码返回二维数组
#include <cstdio>
// Returns a pointer to a newly created 2d array the array2D has size [height x width]
int** create2DArray(unsigned height, unsigned width)
{
int** array2D = 0;
array2D = new int*[height];
for (int h = 0; h < height; h++)
{
array2D[h] = new int[width];
for (int w = 0; w < width; w++)
{
// fill in some initial values
// (filling in zeros would be more logic, but this is just for the example)
array2D[h][w] = w + width * h;
}
}
return array2D;
}
int main()
{
printf("Creating a 2D array2D\n");
printf("\n");
int height = 15;
int width = 10;
int** my2DArray = create2DArray(height, width);
printf("Array sized [%i,%i] created.\n\n", height, width);
// print contents of the array2D
printf("Array contents: \n");
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
printf("%i,", my2DArray[h][w]);
}
printf("\n");
}
// important: clean up memory
printf("\n");
printf("Cleaning up memory...\n");
for (int h = 0; h < height; h++) // loop variable wasn't declared
{
delete [] my2DArray[h];
}
delete [] my2DArray;
my2DArray = 0;
printf("Ready.\n");
return 0;
}
#包括
//返回指向新创建的二维数组的指针,该数组的大小为[height x width]
int**create2DArray(无符号高度、无符号宽度)
{
int**array2D=0;
array2D=新整数*[高度];
对于(int h=0;h
您在函数中所做的任何更改都将保持不变。因此,无需返回任何内容。您可以传递2d数组并随时更改它
void MakeGridOfCounts(int Grid[][6])
{
cGrid[6][6] = {{0, }, {0, }, {0, }, {0, }, {0, }, {0, }};
}
或
int**create2DArray(无符号高度、无符号宽度)
{
int**array2D=0;
array2D=新整数*[高度];
对于(int h=0;h
使用指针到指针的更好替代方法是使用std::vector
。它负责内存分配和释放的细节
std::vector<std::vector<int>> create2DArray(unsigned height, unsigned width)
{
return std::vector<std::vector<int>>(height, std::vector<int>(width, 0));
}
std::vector create2DArray(无符号高度、无符号宽度)
{
返回std::vector(高度,std::vector(宽度,0));
}
返回指向所有行的起始元素的指针数组是返回2D数组的唯一正确方法。
< P>我建议您作为C++的开源工具,其用法与C++中的数组类似。在这里你可以看到
#include <cstdio>
// Returns a pointer to a newly created 2d array the array2D has size [height x width]
int** create2DArray(unsigned height, unsigned width)
{
int** array2D = 0;
array2D = new int*[height];
for (int h = 0; h < height; h++)
{
array2D[h] = new int[width];
for (int w = 0; w < width; w++)
{
// fill in some initial values
// (filling in zeros would be more logic, but this is just for the example)
array2D[h][w] = w + width * h;
}
}
return array2D;
}
int main()
{
printf("Creating a 2D array2D\n");
printf("\n");
int height = 15;
int width = 10;
int** my2DArray = create2DArray(height, width);
printf("Array sized [%i,%i] created.\n\n", height, width);
// print contents of the array2D
printf("Array contents: \n");
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
printf("%i,", my2DArray[h][w]);
}
printf("\n");
}
// important: clean up memory
printf("\n");
printf("Cleaning up memory...\n");
for (int h = 0; h < height; h++) // loop variable wasn't declared
{
delete [] my2DArray[h];
}
delete [] my2DArray;
my2DArray = 0;
printf("Ready.\n");
return 0;
}
矩阵函数名(){
矩阵arr(2,2);
arr[0][0]=5;
arr[0][1]=10;
arr[1][0]=0;
arr[1][1]=44;
返回arr;
}
#包括
使用名称空间std;
typedef int(*Type)[3][3];
类型Demo_函数(类型)//原型
int main(){
cout该函数返回一个静态2D数组
const int N = 6;
int (*(MakeGridOfCounts)())[N] {
static int cGrid[N][N] = {{0, }, {0, }, {0, }, {0, }, {0, }, {0, }};
return cGrid;
}
int main() {
int (*arr)[N];
arr = MakeGridOfCounts();
}
你需要使数组静态,因为它将有块范围,当函数调用结束时,数组将被创建和销毁。静态范围变量持续到程序结束。< /P>你的问题是,这是C还是C++?如果你是新手,试图返回2D数组,你可能需要。数组不是初学者友好的,当它们是多维的时,MichaelKristofik的观点是明智的。但是,这两种语言的技术是合乎情理的。它可能会导致巨大的编译问题。C和C++不是同一种语言。@用户手册1047092:不要忘记<代码>免费< /COD>当你完成它时分配的内存,或者<代码>删除< /代码>。(如果你写C++的标题是这个问题的话)不,如果您使用C++,不要<代码>删除<代码>任何东西。永远使用资源管理类。@ DealMG…如果他分配内存,他应该<代码>删除<代码> >。我不在乎如果你对资源管理类的所有资源都是高跟的,分配应该遵循一个解除分配。在我返回它之后,我应该如何删除它?已经超出范围。@user1047092在完成后,您可以在调用函数中删除它。您的答案有两大缺点:1.您只给出代码,而不给出e
Matrix funcionName(){
Matrix<int> arr(2, 2);
arr[0][0] = 5;
arr[0][1] = 10;
arr[1][0] = 0;
arr[1][1] = 44;
return arr;
}
#include <iostream>
using namespace std ;
typedef int (*Type)[3][3] ;
Type Demo_function( Type ); //prototype
int main (){
cout << "\t\t!!!!!Passing and returning 2D array from function!!!!!\n"
int array[3][3] ;
Type recieve , ptr = &array;
recieve = Demo_function( ptr ) ;
for ( int i = 0 ; i < 3 ; i ++ ){
for ( int j = 0 ; j < 3 ; j ++ ){
cout << (*recieve)[i][j] << " " ;
}
cout << endl ;
}
return 0 ;
}
Type Demo_function( Type array ){/*function definition */
cout << "Enter values : \n" ;
for (int i =0 ; i < 3 ; i ++)
for ( int j = 0 ; j < 3 ; j ++ )
cin >> (*array)[i][j] ;
return array ;
}
const int N = 6;
int (*(MakeGridOfCounts)())[N] {
static int cGrid[N][N] = {{0, }, {0, }, {0, }, {0, }, {0, }, {0, }};
return cGrid;
}
int main() {
int (*arr)[N];
arr = MakeGridOfCounts();
}