How do you return a 2d array in C ++?

How to return a 2d array in C ++?

For example, I have the following method in java:

public static int[][] getFreeCellList(int[][] grid) {
    // Determine the number of free cells
    int numberOfFreeCells = 0;
    for (int i=0; i<9; i++) 
      for (int j=0; j<9; j++)
        if (grid[i][j] == 0)
          numberOfFreeCells++;

    // Store free cell positions into freeCellList
    int[][] freeCellList = new int[numberOfFreeCells][2];
    int count = 0;
    for (int i=0; i<9; i++)
      for (int j=0; j<9; j++)
        if (grid[i][j] == 0) {
          freeCellList[count][0] = i;
          freeCellList[count++][1] = j;
        }
    return freeCellList;
  }

I am trying to reproduce this in C ++. I usually passed in a 2d array, which I would like to return as a reference parameter to a method in C ++.

However, as you see in the method above, the size of the returned array is unknown until runtime.

So, in this case, I assume that I need to return a 2d array, right?

+3
source share
6 answers

You can use vectorfor vector.

typedef vector<vector<int> > array2d_t;

array2d_t etFreeCellList(array2d_t grid) {
    // ...

    array2d_t freeCellList;

    // Determine the number of free cells
    int numberOfFreeCells = 0;
    for (int i=0; i<9; i++) 
       for (int j=0; j<9; j++)
          if (grid[i][j] == 0) {
      freeCellList[count][0] = i;
      freeCellList[count++][1] = j;
    }
    return freeCellList; 
}
+5
source

Internal arrays are apparently tied to size 2. You can use a vector to do this. array

static std::vector< array<int, 2> > getFreeCellList(int grid[][9]) {
    // Determine the number of free cells
    int numberOfFreeCells = 0;
    for (int i=0; i<9; i++) 
      for (int j=0; j<9; j++)
        if (grid[i][j] == 0)
          numberOfFreeCells++;

    // Store free cell positions into freeCellList
    std::vector< array<int, 2> > freeCellList(numberOfFreeCells);
    int count = 0;
    for (int i=0; i<9; i++)
      for (int j=0; j<9; j++)
        if (grid[i][j] == 0) {
          freeCellList[count][0] = i;
          freeCellList[count++][1] = j;
        }
    return freeCellList;
}

Usage is similar to

int x[9][9] = { ... };
std::vector< array<int, 2> > pa = getFreeCellList(x);

, std::vector. array boost,

template<typename E, int N>
struct array {
  E &operator[](int I) { return data[I]; }
  E data[N];
};

N E.

, 2d-. , , 2d- 1d- :

static identity<int[2]>::type *getFreeCellList(int grid[][9]) {
    // Determine the number of free cells
    int numberOfFreeCells = 0;
    for (int i=0; i<9; i++) 
      for (int j=0; j<9; j++)
        if (grid[i][j] == 0)
          numberOfFreeCells++;

    // Store free cell positions into freeCellList
    int (*freeCellList)[2] = new int[numberOfFreeCells][2];
    int count = 0;
    for (int i=0; i<9; i++)
      for (int j=0; j<9; j++)
        if (grid[i][j] == 0) {
          freeCellList[count][0] = i;
          freeCellList[count++][1] = j;
        }
    return freeCellList;
}

int x[9][9] = { ... };

// equivalent: identity<int[2]>::type *pa = ...;
int (*pa)[2] = getFreeCellList(x);
// ...
delete[] pa;

identity ( boost . ), .

static int (*getFreeCellList(int grid[][9]))[2] {
  // ...
}

// identity implementation (for working around the evil C++ syntax)
template<typename T>
struct identity { typedef T type; };
+1

. ++ . ( 1D 2D-).

0

2d , , 2d .

++ Java. ! vector.

0

int[][] 

C ++ ,

int[][] =
{
    {1, 2},
    {0, 1}
};

. - , typedef, :

typedef int[16] IntArr16;

IntArr16* getFreeCellList(...).

C. ( dirkgently), stl .

0

C-, ++, ,

struct {
    int   dimension_one;
    int   dimension_two;
    void* pArray;
} return_type_t;

2D-, .

0

Source: https://habr.com/ru/post/1734619/


All Articles