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

如何将符号文本文件转换为对应数值的二维std::vector(C++)

问题描述

正在学习C++,此前编写了一段代码可将整数格式的文本文件内容存入二维std::vector,代码如下:

std::vector<std::vector<int>> sprites;
std::ifstream file("initialroom.txt");
std::string line;

for (line; std::getline(file, line);){
    sprites.push_back(std::vector<int>());
    std::stringstream ss(line);
    std::copy(std::istream_iterator<int>(ss), std::istream_iterator<int>(), std::back_inserter(sprites.back()));
}

该代码可正常读取initialroom.txt文件(内容如下):

initialroom.txt:

0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0
0 1 2 1 1 1 1 0 1 0
0 1 2 1 1 1 1 0 1 0
0 1 1 1 1 3 1 0 0 0
0 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 0
0 1 2 2 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1 0 
0 0 0 0 0 0 0 0 0 0

现在需要改为读取符号格式的initialroomdesired.txt文件(内容如下):

initialroomdesired.txt:

# # # # # # # # # #
# . . . . . . . . # 
# . & . . . . # . #
# . & . . . . # . # 
# . . . . $ . # # #
# . . . . . . . . #
# . . . . . . . . #
# . & & . . . . . # 
# . . . . . . . . # 
# # # # # # # # # #  

要求将文件中的符号按规则映射为数值(#=0,.=1,&=2,$=3)后存入同结构的二维vector。尝试用switch语句处理stringstream时出现指针相关错误,无法实现功能,求解决方案。

解决方案

原代码通过std::istream_iterator<int>直接读取整数,现在需要读取符号字符串并映射为数字,核心是替换整数读取逻辑为符号读取+映射的逻辑,以下提供两种可行实现:

方法一:使用映射表(简洁易维护)

通过std::unordered_map建立符号到数字的映射关系,逐个读取文件中的符号字符串,转换后存入vector:

#include <vector>
#include <fstream>
#include <string>
#include <sstream>
#include <unordered_map>

int main() {
    std::vector<std::vector<int>> sprites;
    std::ifstream file("initialroomdesired.txt");
    std::string line;

    // 定义符号-数值映射表
    std::unordered_map<char, int> symbolMap = {
        {'#', 0},
        {'.', 1},
        {'&', 2},
        {'$', 3}
    };

    while (std::getline(file, line)) {
        sprites.emplace_back(); // 等价于push_back空vector,更高效
        std::stringstream ss(line);
        std::string symbol;
        // 逐个读取每行中的符号(空格分隔)
        while (ss >> symbol) {
            // 取符号的第一个字符(每个符号为单个字符的字符串)
            char c = symbol.front();
            // 映射后存入vector,可添加默认处理应对未知符号
            sprites.back().push_back(symbolMap.at(c));
        }
    }

    // 可选:验证输出结果
    for (const auto& row : sprites) {
        for (int num : row) {
            std::cout << num << " ";
        }
        std::cout << "\n";
    }

    return 0;
}

方法二:使用switch语句(无需额外容器)

如果倾向于用switch处理,需确保判断的是字符类型(而非字符串),代码如下:

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

int main() {
    std::vector<std::vector<int>> sprites;
    std::ifstream file("initialroomdesired.txt");
    std::string line;

    while (std::getline(file, line)) {
        sprites.emplace_back();
        std::stringstream ss(line);
        std::string symbol;
        while (ss >> symbol) {
            char c = symbol.front();
            int num;
            // 对单个字符做switch判断
            switch (c) {
                case '#': num = 0; break;
                case '.': num = 1; break;
                case '&': num = 2; break;
                case '$': num = 3; break;
                default: num = 0; // 未知符号默认值,可按需调整
            }
            sprites.back().push_back(num);
        }
    }

    // 可选:验证输出结果
    for (const auto& row : sprites) {
        for (int num : row) {
            std::cout << num << " ";
        }
        std::cout << "\n";
    }

    return 0;
}

错误原因说明

你之前用switch出现指针错误,大概率是尝试直接对std::string对象做switch判断(switch仅支持整数类型),或者错误处理了字符串的指针/引用。上述两种方法都先提取符号字符串的第一个字符(symbol.front()或symbol[0]),确保switch判断的是char类型(整数类型的一种),即可避免此类错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 21:55:45