拆分包含逗号分隔条目的字符串流

时间:2012-11-24 19:31:17

标签: c++ string stream operator-overloading stringstream

我有两个字符串,如下所示:

string text1 = "you,are,good";
string text2 = "1,2,3,4,5";
stringstream t1(text1);
stringstream t2(text2);

我使用以下代码将其解析为逗号分隔数据

template <typename T>
std::istream &operator>>(std::istream &is, Array<T> &t)
{
    T i;
    while (is >> i)
    {
        t.push_back(i);

        if (is.peek() == ',')
            is.ignore();
    }
    return is;
}

其中&#34;是&#34;是t1或t2。这将text2分开但与text1失败。你能帮助我吗,告诉我为什么它不能用弦? 我需要一个能解析字符串和数字的通用代码。

感谢您的任何努力:)

2 个答案:

答案 0 :(得分:3)

如果你需要用逗号分割字符串,我所知道的最简单的方法是重新定义流的空间含义。这很容易替换std::ctype<char>方面。这是我之前发布的这个版本...

#include <iostream>
#include <iterator>
#include <string>
#include <set>
#include <algorithm>

using namespace std;

typedef string T; // to simplify, always consider T as string

template<typename input_iterator>
void do_something(const input_iterator& first, const input_iterator& last) {
    const ostream_iterator<T> os(cout, "\n");
    const set<T> words(first, last);
    copy(words.begin(), words.end(), os);
}

#include <locale>
template <char S0, char S1>
struct commactype_base {
    commactype_base(): table_() {
        std::transform(std::ctype<char>::classic_table(),
                       std::ctype<char>::classic_table() + std::ctype<char>::table_size,
                       this->table_, 
                       [](std::ctype_base::mask m) -> std::ctype_base::mask {
                           return m & ~(std::ctype_base::space);
                       });
        this->table_[static_cast<unsigned char>(S0)] |= std::ctype_base::space;
        this->table_[static_cast<unsigned char>(S1)] |= std::ctype_base::space;
    }
    std::ctype<char>::mask table_[std::ctype<char>::table_size];
    static std::ctype_base::mask clear_space(std::ctype_base::mask m) {
        return m & ~(std::ctype_base::space);
    }
};
template <char S0, char S1 = S0>
struct ctype:
    commactype_base<S0, S1>,
    std::ctype<char>
{
    ctype(): std::ctype<char>(this->table_, false) {}
};

int main() {
    std::cin.imbue(std::locale(std::locale(), new ::ctype<',', '\n'>));
    const istream_iterator<T> is(cin), eof;
    do_something(is, eof);
    return 0;
}

答案 1 :(得分:3)

当应用于字符串时,istream >>运算符会丢弃最终的初始空格并读取第一个“空格”。

对于任何类型(包括int),它都是相同的。它适用于您的代码,因为在','“int reader”失败,并假定以下内容是其他内容。

读取逗号分隔字符串的最简单方法是使用std::getline函数,将','作为分隔符。

在您的情况下,您的模板功能

template <typename T>
std::istream &operator>>(std::istream &is, Array<T> &t)
{ ...... }

仍然有效,但需要专业化

std::istream &operator>>(std::istream &is, Array<std::string> &t)
{
    std::string r;
    while(std::getline(is,r,','))
        t.push_back(r);
    return is;
}
相关问题