简体   繁体   中英

C++ 2D array to function when size is variable

I need to pass 2D array of struct to function.

struct Point {  
  int x, y;
  double f, g, h;
  int parentX, parentY;
  int status;
};

int main(int argc, char* argv[])
{
  Point grid[fieldX][fieldY];
  void something(grid){}
}

How to?

You have plenty of choices:

Use std::vector this makes it super easy:

std::vector<std::vector<Point>> Grid;

void something(std::vector<std::vector<Point>> &grid) { ...

If you know the size is going to be set at compile time always, use a std::array :

typedef std::array<std::array<Point, YDimSize>, XDimSize> Grid;
Grid grid;

void something(Grid &grid) { ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM