如何让std :: istringstream将给定的字符视为空格?

时间:2013-03-29 16:29:40

标签: c++ parsing stringstream

使用std::istringstream可以轻松阅读由空格分隔的单词。但要解析以下行,我需要将字符/视为空格。

f 104/387/104 495/574/495 497/573/497

如何读取由斜杠或空格分隔的值?

4 个答案:

答案 0 :(得分:8)

一种方法是定义一个将/分类为空格的ctype方面:

class my_ctype : public std::ctype<char> {
public:
    mask const *get_table() { 
        static std::vector<std::ctype<char>::mask> 
            table(classic_table(), classic_table()+table_size);
        table['/'] = (mask)space;
        return &table[0];
    }
    my_ctype(size_t refs=0) : std::ctype<char>(get_table(), false, refs) { }
};

从那里,使用该ctype facet使用语言环境填充流,然后读取单词:

int main() { 
    std::string input("f 104/387/104 495/574/495 497/573/497");
    std::istringstream s(input);
    s.imbue(std::locale(std::locale(), new my_ctype));

    std::copy(std::istream_iterator<std::string>(s),
              std::istream_iterator<std::string>(),
              std::ostream_iterator<std::string>(std::cout, "\n"));
}

答案 1 :(得分:4)

如果提升可用,那么boost::split()将是一种可能的解决方案。使用std::getline()填充std::string,然后拆分该行:

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

#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>

int main()
{
    std::vector<std::string> tokens;
    std::string line("f 104/387/104 495/574/495 497/573/497");
    boost::split(tokens, line, boost::is_any_of("/ "));

    for (auto& token: tokens) std::cout << token << "\n";

    return 0;
}

输出:

f
104
387
104
495
574
495
497
573
497

答案 2 :(得分:2)

如果你知道何时用斜线或空格分割,你可以使用std::getline

std::istringstream is("f 104/387/104 495/574/495 497/573/497");
std::string f, i, j, k;
std::getline(is, f, ' ');
std::getline(is, i, '/');
std::getline(is, j, '/');
std::getline(is, k, ' ');

或者,您可以使用格式化输入并手动丢弃斜杠

std::string f;
int i, j, k;
char slash;
is >> f >> i >> slash >> j >> slash >> k;

答案 3 :(得分:0)

我确信这不是最好的方式,但我正在编写编程原理和练习使用C ++第二版。作者:Bjarne Stroustrup ,我想出了一个可能适合你的解决方案。我四处搜索,看看别人是怎么做的(这就是我找到这个帖子的方式),但我真的没找到任何东西。

首先,这是本书的练习:

  

写一个函数向量&lt; string&gt; split(const string&amp; s,const string&amp;   w)返回一个由空格分隔的子串的向量   参数s,其中空格被定义为“普通空白”加   w。中的人物。

这是我提出的解决方案,似乎运作良好。我试着评论它以使其更清楚。只是想提一下我对C ++很新(这就是我读这本书的原因),所以不要对我太过刻意。 :)

// split a string into its whitespace-separated substrings and store
// each string in a vector<string>. Whitespace can be defined in argument
// w as a string (e.g. ".;,?-'")
vector<string> split(const string& s, const string& w)
{
    string temp{ s };
    // go through each char in temp (or s)
    for (char& ch : temp) {     
        // check if any characters in temp (s) are whitespace defined in w
        for (char white : w) {  
            if (ch == white)
                ch = ' ';       // if so, replace them with a space char ('')
        }
    }

    vector<string> substrings;
    stringstream ss{ temp };

    for (string buffer; ss >> buffer;) {
        substrings.push_back(buffer);
    }
    return substrings;
}

然后你可以做这样的事情来使用它:

cout << "Enter a string and substrings will be printed on new lines:\n";
string str;
getline(cin, str);
vector<string> substrings = split(str, ".;,?-'");

cout << "\nSubstrings:\n";
for (string s : substrings)
    cout << s << '\n';

我知道你不想分割字符串,但这只是你如何将其他字符视为空格的一个例子。基本上,我只是用''替换那些字符,所以它们确实变成了空白。当使用流时,它工作得很好。 for循环可能是您案例的相关代码。

相关问题