我这样做对吗,我想要一个以 Integer 作为键,以 struct 作为值的映射。什么是最简单的方法,比如说我想要1
. 如何检索 的值isIncluded
?代码中的最后两行,我试过这样做,但后来我意识到我真的不知道在编号的 Map 数组中检索结构值的方法是什么。
我是否需要调用cells.get(1)
并将其分配给一个新的临时结构以获取其值?
/** set ups cells map. with initial state of all cells and their info*/
void setExcludedCells (int dimension)
{
// Sets initial state for cells
cellInfo getCellInfo;
getCellInfo.isIncluded = false;
getCellInfo.north = 0;
getCellInfo.south = 0;
getCellInfo.west = 0;
getCellInfo.east = 0;
for (int i = 1; i <= (pow(dimension, 2)); i++)
{
cells.put(i, getCellInfo);
}
cout << "Cells map initialized. Set [" << + cells.size() << "] cells to excluded: " << endl;
cells.get(getCellInfo.isIncluded);
cells.get(1);
}
Map 被声明为私有实例变量,如下所示:
struct cellInfo {
bool isIncluded;
int north; // If value is 0, that direction is not applicable (border of grid).
int south;
int west;
int east;
};
Map<int, cellInfo> cells; // Keeps track over included /excluded cells