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

如何创建带空值与递减编号的2D数组网格并读取.txt文件数据?

解决网格创建与城市数据填充的问题

看起来你在创建网格地图和处理城市数据时遇到了几个具体问题,我来一步步帮你解决:

一、先修复网格绘制的逻辑

你的现有代码在网格对齐、侧边/底部编号的位置上有偏差,我们先把网格框架调整到符合预期的样子:

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <string>
#include <cerrno>

using namespace std;

int main() {
    const int GridX = 12, GridY = 12;
    int cityMap[GridX][GridY] = {0}; // 初始化网格为0,空位置后续替换为空格

    // --- 先绘制网格框架(后续再填充数据)---
    // 1. 绘制首行的#分隔线
    cout << "  "; // 预留侧边编号的位置
    for (int i = 0; i < GridX; i++) {
        cout << " # ";
    }
    cout << endl;

    // 2. 绘制中间9行(对应Y轴8到0,侧边显示Y编号)
    for (int y = 8; y >= 0; y--) {
        cout << y << " "; // 侧边的Y轴编号
        for (int x = 0; x < GridX; x++) {
            // 先默认输出空格占位,后续替换为cityID
            cout << "   ";
        }
        cout << endl;
    }

    // 3. 绘制末行的#分隔线
    cout << "  ";
    for (int i = 0; i < GridX; i++) {
        cout << " # ";
    }
    cout << endl;

    // 4. 绘制底部的X轴编号(0-8,对应网格第3到第11位)
    cout << "   "; // 对齐前面的空格
    for (int x = 0; x <= 8; x++) {
        cout << " " << x << " ";
    }
    // 补全剩余列的空格,保证网格宽度一致
    for (int i = 9; i < GridX; i++) {
        cout << "   ";
    }
    cout << endl;

    return 0;
}

调整说明:

  • 首末行预留了2字符的侧边编号位置,和中间行的Y编号对齐
  • 每个单元格用3字符宽度(" "),确保网格整体对齐
  • 底部X编号从第3位开始,刚好对应网格的X轴0-8位置,后续补空格保证宽度一致

二、读取并解析样本.txt文件

接下来处理文件读取和数据分割,用stringstream按-拆分每行内容,提取坐标和cityID:

把这段代码插入到网格绘制之前(也就是// --- 先绘制网格框架 ---的上方):

// --- 读取并解析样本.txt,填充cityMap ---
ifstream file("样本.txt");
if (!file.is_open()) {
    cerr << "无法打开文件:" << strerror(errno) << endl;
    return 1;
}

string line;
while (getline(file, line)) {
    stringstream ss(line);
    string part;
    vector<string> parts;

    // 按'-'分割每行内容
    while (getline(ss, part, '-')) {
        parts.push_back(part);
    }

    // 校验行格式是否正确
    if (parts.size() != 3) {
        cerr << "跳过格式错误的行:" << line << endl;
        continue;
    }

    // 解析坐标[X,Y]
    string coord = parts[0];
    if (coord.front() != '[' || coord.back() != ']') {
        cerr << "坐标格式错误:" << line << endl;
        continue;
    }
    coord = coord.substr(1, coord.size() - 2); // 去掉首尾的[]
    stringstream coord_ss(coord);
    char comma;
    int x, y;
    if (!(coord_ss >> x >> comma >> y) || comma != ',') {
        cerr << "坐标解析失败:" << line << endl;
        continue;
    }

    // 解析cityID
    int cityID;
    try {
        cityID = stoi(parts[1]);
    } catch (const invalid_argument& e) {
        cerr << "cityID不是有效数字:" << line << endl;
        continue;
    } catch (const out_of_range& e) {
        cerr << "cityID超出范围:" << line << endl;
        continue;
    }

    // 将cityID存入对应网格位置
    if (x >= 0 && x < GridX && y >= 0 && y <= 8) {
        cityMap[x][y] = cityID;
    } else {
        cerr << "坐标(" << x << "," << y << ")超出网格范围,跳过:" << line << endl;
    }
}
file.close();

解析说明:

  • 增加了多维度的错误处理(文件打开失败、行格式错误、坐标解析失败、cityID转换失败)
  • 严格校验坐标格式,确保提取的X/Y是有效数字
  • 检查坐标是否在网格范围内,避免数组越界

三、整合完整代码并优化显示

最后把网格绘制部分的空占位替换为实际的cityID,空位置显示空格:

修改中间行的绘制代码:

// 2. 绘制中间9行(对应Y轴8到0,侧边显示Y编号)
for (int y = 8; y >= 0; y--) {
    cout << y << " "; // 侧边的Y轴编号
    for (int x = 0; x < GridX; x++) {
        // 空位置显示空格,有数据显示cityID
        if (cityMap[x][y] == 0) {
            cout << "   ";
        } else {
            cout << " " << cityMap[x][y] << " ";
        }
    }
    cout << endl;
}

四、测试示例

假设你的样本.txt包含一行:[2,5]-123-北京,运行代码后,网格中X=2、Y=5的位置会显示123,其他位置显示空格,首末行是连续的#,侧边是8到0的编号,底部是0到8的水平编号,完全符合你的预期。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:57:15