试图了解如何将 C++ 对转换为 C 代码

Trying to understand how C++ pairs can be converted to C code

所以我正在尝试做一个小测试项目,我正在观看一个关于如何制作部分代码的教程,问题是,在视频中,用户使用 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像这样:

return (struct pair_int_int){line, column};

您可以将 pair 视为 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;
}