只要你使用 C++...
#include <iostream>
using namespace std;
struct Tile
{
bool mine, visible;
int danger;
};
// load a square board of declared-size.
template<size_t N>
void loadboard( Tile (&board)[N][N], const std::string& filename)
{
// load board here.
cout << "Loading from file: " << filename << endl;
for (size_t i=0;i<N;++i)
{
cout << "board[" << i << "]: [ ";
for (size_t j=0;j<N;++j)
{
// load element board[i][j] here
cout << j << ' ';
}
cout << ']' << endl;
}
cout << endl;
}
int main()
{
Tile board[6][6];
loadboard(board, "yourfilename.bin"); // OK dims are the same
Tile smaller[3][3];
loadboard(smaller, "anotherfile.bin"); // OK. dims are the same
// Tile oddboard[5][6];
// loadboard(oddboard, "anotherfile.bin"); // Error: dims are not the same.
return 0;
}
输出
Loading from file: yourfilename.bin
board[0]: [ 0 1 2 3 4 5 ]
board[1]: [ 0 1 2 3 4 5 ]
board[2]: [ 0 1 2 3 4 5 ]
board[3]: [ 0 1 2 3 4 5 ]
board[4]: [ 0 1 2 3 4 5 ]
board[5]: [ 0 1 2 3 4 5 ]
Loading from file: anotherfile.bin
board[0]: [ 0 1 2 ]
board[1]: [ 0 1 2 ]
board[2]: [ 0 1 2 ]
当然,也可能有“特定说明”不使用该语言的模板功能。再说一次,我敢打赌,这些说明也不包括让 SO 用户解决您的问题,所以我不会对那些很快就会严格遵循的说明进行评估。