嗨,我是C ++的新手 我试图从函数返回一个二维数组。 它是这样的
int **MakeGridOfCounts(int Grid[][6])
{
int cGrid[6][6] = {{0, }, {0, }, {0, }, {0, }, {0, }, {0, }};
return cGrid;
}
答案 0 :(得分: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 ( h = 0; h < height; h++)
{
delete [] my2DArray[h];
}
delete [] my2DArray;
my2DArray = 0;
printf("Ready.\n");
return 0;
}
答案 1 :(得分:4)
你在你的片段中做什么(尝试做)/做什么是从函数中返回一个局部变量,这根本不是推荐的 - 也不是根据标准允许的。
如果您想从函数中创建int[6][6]
,您必须在免费商店中为其分配内存(即使用 new T / malloc 或类似的功能),或将已经分配的内存传递给MakeGridOfCounts
。
答案 2 :(得分:4)
该代码不起作用,如果我们修复它,它不会帮助你学习正确的C ++。如果你做了不同的事情,那就更好了。原始数组(尤其是多维数组)很难正确地传递到函数和从函数传递。我认为从一个代表数组但可以安全复制的对象开始,你会好得多。查看std::vector
的文档。
在您的代码中,您可以使用vector<vector<int> >
,也可以使用36个元素vector<int>
来模拟二维数组。
答案 3 :(得分:4)
使用指针指针的更好选择是使用std::vector
。这会处理内存分配和释放的细节。
std::vector<std::vector<int>> create2DArray(unsigned height, unsigned width)
{
return std::vector<std::vector<int>>(height, std:vector<int>(width, 0));
}
答案 4 :(得分:1)
#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 ;
}
答案 5 :(得分:0)
你在功能上做的任何改变都会持续存在。所以你不需要返回任何东西。你可以传递2d数组并随时改变它。
void MakeGridOfCounts(int Grid[][6])
{
cGrid[6][6] = {{0, }, {0, }, {0, }, {0, }, {0, }, {0, }};
}
或
void MakeGridOfCounts(int Grid[][6],int answerArray[][6])
{
....//do the changes in the array as you like they will reflect in main...
}
答案 6 :(得分:0)
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");
}
return 0;
}
答案 7 :(得分:0)
返回指向所有行的起始元素的指针数组是返回2d数组的唯一方法。
答案 8 :(得分:0)
我建议你Matrix library作为c ++的开源工具,它的用法就像c ++中的数组一样。在这里,您可以看到documention。
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;
}
答案 9 :(得分:0)
该函数返回静态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();
}
您需要使数组静态,因为它将具有块作用域,当函数调用结束时,将创建并销毁数组。静态作用域变量持续到程序结束。