You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C++控制台打印带内边距的双层嵌套网格?

C++控制台双层嵌套网格实现与替代方案

核心实现思路

控制台输出是逐行流式的,因此嵌套网格不能按单元格逐个渲染,必须按行组处理:每个父网格行对应子网格的N行(此处为3行),将同一行组内所有父单元格的对应子行拼接后输出,同时保留单元格和父行之间的内边距。

代码实现

子网格行生成函数

负责生成子网格的3行内容,分别对应北、西/东、南向数据:

#include <vector>
#include <string>
#include <iostream>
#include <tuple>

// 生成子网格的三行内容,参数依次为北(a)、西(b)、东(c)、南(d)数据
std::vector<std::string> generateChildGridLines(int a, int b, int c, int d) {
    return {
        "  " + std::to_string(a),                  // 第一行:北向数据
        std::to_string(b) + "   " + std::to_string(c), // 第二行:西+东向数据
        "  " + std::to_string(d)                   // 第三行:南向数据
    };
}

父网格打印函数

遍历父网格的行与列,逐行组拼接子网格内容并添加内边距:

// 打印嵌套网格,cellPadding为单元格间空格数,rowPadding为父行间空行数
void printParentGrid(const std::vector<std::vector<std::tuple<int, int, int, int>>>& parentGrid, 
                     int cellPadding = 3, int rowPadding = 1) {
    const int childRowCount = 3;
    const int parentRowCount = parentGrid.size();
    if (parentRowCount == 0) return;
    const int parentColCount = parentGrid[0].size();

    for (int parentRow = 0; parentRow < parentRowCount; ++parentRow) {
        // 遍历子网格的每一行
        for (int childRow = 0; childRow < childRowCount; ++childRow) {
            std::string currentLine;
            // 拼接当前父行所有单元格的对应子行
            for (int parentCol = 0; parentCol < parentColCount; ++parentCol) {
                auto [a, b, c, d] = parentGrid[parentRow][parentCol];
                auto childLines = generateChildGridLines(a, b, c, d);
                currentLine += childLines[childRow];
                // 添加单元格内边距(最后一列除外)
                if (parentCol != parentColCount - 1) {
                    currentLine += std::string(cellPadding, ' ');
                }
            }
            std::cout << currentLine << '\n';
        }
        // 添加父行内边距(最后一行除外)
        if (parentRow != parentRowCount - 1) {
            for (int i = 0; i < rowPadding; ++i) {
                std::cout << '\n';
            }
        }
    }
}

测试代码

创建3x3父网格并打印,效果与示例一致:

int main() {
    // 初始化3x3父网格,每个子网格用1(北)、2(西)、3(东)、4(南)测试
    std::vector<std::vector<std::tuple<int, int, int, int>>> parentGrid(
        3, std::vector<std::tuple<int, int, int, int>>(3, {1, 2, 3, 4})
    );
    printParentGrid(parentGrid, 5); // 设置单元格间距为5个空格,匹配示例格式
    return 0;
}

控制台对齐问题的替代方案

如果控制台使用非等宽字体导致网格对齐混乱,可采用紧凑分隔式网格,将每个单元格的多维度数据打包显示,用分隔线保证可读性:

替代方案代码

// 紧凑版网格打印,适配非等宽控制台
void printCompactParentGrid(const std::vector<std::vector<std::tuple<int, int, int, int>>>& parentGrid) {
    const int parentRowCount = parentGrid.size();
    if (parentRowCount == 0) return;
    const int parentColCount = parentGrid[0].size();

    // 生成横向分隔线
    std::string divider;
    for (int col = 0; col < parentColCount; ++col) {
        divider += "+-----------------";
    }
    divider += "+";

    // 逐行打印网格
    for (int parentRow = 0; parentRow < parentRowCount; ++parentRow) {
        std::cout << divider << '\n';
        std::cout << "|";
        for (int parentCol = 0; parentCol < parentColCount; ++parentCol) {
            auto [a, b, c, d] = parentGrid[parentRow][parentCol];
            std::cout << " N:" << a << " W:" << b << " E:" << c << " S:" << d << " |";
        }
        std::cout << '\n';
    }
    std::cout << divider << '\n';
}

在main函数中调用printCompactParentGrid(parentGrid)即可输出紧凑格式。

内容的提问来源于stack exchange,提问作者TheDarknesS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 09:28:16