从 C++ 中的函数返回一个二维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15908632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:52:56  来源:igfitidea点击:

returning a two dimensional array from a function in c++

c++arraysreturn-type

提问by MrHs

I want to use a two dimensional int array which is returned from a function how should I define the function return value ? I used int** but the compiler gave error:

我想使用从函数返回的二维 int 数组,我应该如何定义函数返回值?我使用了 int** 但编译器给出了错误:

int**  tableCreator(){
    int** table=new int[10][10];
    for(int xxx=1;xxx<10;xxx++){
        for(int yyy=1;yyy<10;yyy++){
            table[xxx][yyy]=xxx*yyy;
        }
    }
    return(table);  //Here:cannot convert from 'int (*)[10]' to 'int **'
}

回答by mohaps

Try this:

尝试这个:

#include <cstdio>
#include <cstdlib>


int** createTable(int rows, int columns){
    int** table = new int*[rows];
    for(int i = 0; i < rows; i++) {
        table[i] = new int[columns]; 
        for(int j = 0; j < columns; j++){ table[i][j] = (i+j); }// sample set value;    
    }
    return table;
}
void freeTable(int** table, int rows){
    if(table){
        for(int i = 0; i < rows; i++){ if(table[i]){ delete[] table[i]; } }
        delete[] table;    
    }
}
void printTable(int** table, int rows, int columns){
    for(int i = 0; i < rows; i++){
        for(int j = 0; j < columns; j++){
            printf("(%d,%d) -> %d\n", i, j, table[i][j]);
        }    
    }
}
int main(int argc, char** argv){
    int** table = createTable(10, 10);
    printTable(table, 10, 10);
    freeTable(table, 10);
    return 0;
}

You need the second loop to allocate a 2-d array in C and similar operation to free it. a two-D array is in essence an array of arrays so can be expressed as a pointer array. the loop initializes the arrays pointed to the pointers.

您需要第二个循环在 C 中分配一个二维数组,并需要类似的操作来释放它。二维数组本质上是数组的数组,因此可以表示为指针数组。循环初始化指向指针的数组。

Clarifying as per conversation with @Eric Postpischil below: changed createTable to take row/column count for truly dynamic allocation.

根据下面与@Eric Postpischil 的对话进行澄清:更改 createTable 以获取行/列计数以进行真正的动态分配。

回答by taocp

int** table=new int[10][10];

this is wrong. you cannot allocate space for 2D dynamic array in this way in C/C++.

这是错误的。您不能在 C/C++ 中以这种方式为二维动态数组分配空间。

Meanwhile, you declared array size as 10, so indices are from 0-9, but you are trying to assign values to index 10in your nested for loops, which is not right too.

同时,您将数组大小声明为10,因此索引来自0-9,但是您试图10在嵌套的 for 循环中为索引分配值,这也不正确。

You may do the following for allocation:

您可以执行以下操作进行分配:

int** table = new int*[10];
for (int i = 0; i < 10; ++i)
{
   table[i] = new int[10];
}

回答by Eric Postpischil

Usually, the type used to point to an array is a pointer to an element of the array. Since a two-dimensional array of intis an array of array of int, you want a pointer to array of int. The C++ syntax for this type is int (*)[N], for some dimension N. This code demonstrates:

通常,用于指向数组的类型是指向数组元素的指针。由于二维数组 ofint是数组 of 的数组int,所以您需要一个指向数组的指针intint (*)[N]对于某些维度,此类型的 C++ 语法是N。此代码演示:

#define N   10

int (*tableCreator())[N]
{
    int (*table)[N] = new int[N][N];

    for (int i = 0; i < N; ++i)
        for (int j = 0; j < N; ++j)
            table[i][j] = i*j;

    return table;
}


#include <iostream>


int main()
{
    int (*t)[N] = tableCreator();

    for (int i = 0; i < N; ++i)
    {
        for (int j = 0; j < N; ++j)
            std::cout << t[i][j] << ' ';
        std::cout << '\n';
    }

    delete [] t;

    return 0;
}

回答by MrHs

I. Arrays are not pointers.

I.数组不是指针。

II. Why not vector<vector<int> >?

二、为什么不vector<vector<int> >呢?

III. If not, then:

三、如果没有,那么:

typedef int Int10Array[10];
Int10Array *arr = new Int10Array[10];

IV. Why write past the bounds? Do you want explicit nasal demons?

四、为什么要写越界?你想要明确的鼻妖吗?

for(int xxx = 0; xxx < 10; xxx++)
             ^^^      ^^^^