簡體   English   中英

C++ 2D 數組在大小可變時起作用

[英]C++ 2D array to function when size is variable

我需要將結構的二維數組傳遞給函數。

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){}
}

如何?

你有很多選擇:

使用std::vector這使它變得非常簡單:

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

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

如果您知道將始終在編譯時設置大小,請使用std::array

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

void something(Grid &grid) { ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM