c++ - 增量联盟?

标签 c++ unions

我试图用 union 语句在 C++ 中描述数独板:

union Board
{
    int board[9][9];
    int sec1[3][3];
    int sec2[3][3];
    int sec3[3][3];
    int sec4[3][3];
    int sec5[3][3];
    int sec6[3][3];
    int sec7[3][3];
    int sec8[3][3];
    int sec9[3][3];     
}

电路板的每个部分是否与阵列的正确部分相对应?浏览器,

sec4 会对应 board[4-6][0-3] 吗?有没有更好的方法来做这类事情(特别是描述数独板)?

最佳答案

你可以通过将它封装在一个类中来实现你想要的效果:

class Board {
public:
    int& sec1(int r, int c) { return board[r][c]); }
    int& sec2(int r, int c) { return board[r][c+3]; }
    // etc.

private:
    int board[9][9];
};

但是,我不确定这是表示数独棋盘的最佳方式。您可能会发现,一旦您开始研究逻辑,您就会想出更好的表示。

关于c++ - 增量联盟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2682408/

相关文章:

c - 为什么这个 union 的大小是 2 位域?

C++ 格式化字符串宏

c++ - C/C++ 中的“#define AP_MODULE_DECLARE(type) 类型”

c++ - 如何使用 scanf 和 C++ 扫描字符串

c++ - 如何将动态 dll 转换为静态库?

c++ - Qt connect() 用法

作为 union 成员的类型可以为该 union 起别名吗?

c - 如何区分union的内容

c - 调用双变量后,对存储为 union 的数据进行 Printf 不会给出任何输出

c - 如何创建 union