首页 > 解决方案 > 如何在 C++ 中将一个字符串分成多个字符串?

问题描述

首先,我将解释我要存档的内容:假设以下字符串, 25a0: ebfffa01 bl dac <abort@plt>(注意第一个字符之前有空格),那么我的目标是获得以下输出:

25a0:
ebfffa01
bl
dac
<abort@plt>

问题是字符串可以被几个(连续的)空格分隔,因此我当前的解决方案无法正常工作:

vector<vector<string>> getStructuredContent(vector<string> content) {
    vector<vector<string>> structuredContent;
    for(string line: content) {
        char space_char = ' ';
        vector<string> words{};
        stringstream sstream(line);
        string word;
        while (getline(sstream, word, space_char)){
            if(word != "")
            words.push_back(word);
        }
        structuredContent.push_back(words);
    }
    return structuredContent;
}

上面字符串的输出当前是:

25a0:   ebfffa01
        bl      dac
<abort@plt>

也许有人知道如何解决这个问题?

标签: c++stringsplit

解决方案


推荐阅读