因此,我正在尝试做一个小测试项目,我正在观看有关如何制作部分代码的教程,问题是,在用户使用C ++的视频上,我正在使用C。我试图弄清楚它并进行了搜索,但是它仍然让我感到困惑。
所以我无法理解的代码是:
pair<int, int> generateUnPos() {
int occupied = 1;
int line, column;
while(occupied){
line = rand() % 4;
column = rand() %4;
if(board[line][column] == 0){
occupied = 0;
}
}
return make_pair(line, column);
}
我知道这与结构有关,但我不知道。有人可以帮我吗?
感谢您的宝贵时间。
模板在C中不存在,因此您需要像这样创建自定义类型:
struct pair_int_int {
int first;
int second;
};
然后像这样返回它:
return (struct pair_int_int){line, column};
您可以在C中将对视为结构。
typedef struct {
int line;
int column;
} Position;
然后这段代码应该是:
Position generateUnPos() {
int occupied = 1;
int line, column;
while (occupied) {
line = rand() % 4;
column = rand() % 4;
if (board[line][column] == 0) {
occupied = 0;
}
}
Position pos = {line, column};
return pos;
}