【问题标题】:boost tokenizer / char separator增强标记器/字符分隔符
【发布时间】:2016-12-12 20:45:34
【问题描述】:

我已经尝试了代码的注释和未注释版本:

string separator1(""); //dont let quoted arguments escape themselves
string separator2(",\n"); //split on comma and newline
string separator3("\"\'"); //let it have quoted arguments

escaped_list_separator<char> els(separator1, separator2, separator4);
tokenizer<escaped_list_separator<char>> tok(str);//, els);


for (tokenizer<escaped_list_separator<char>>::iterator beg = tok.begin();beg!= tok.end(); ++beg) {
next = *beg;
boost::trim(next);
cout << counter << " " << next << endl;
counter++;
}

分离具有以下格式的文件:

 12345, Test Test, Test
 98765, Test2 test2, Test2

这是输出

0 12345
1 Test Test
2 Test
98765
3 Test2 test2
4 Test2

我不确定问题出在哪里,但我需要实现的是在 98765 之前有一个数字 3

【问题讨论】:

    标签: c++ boost tokenize


    【解决方案1】:

    您忘记了换行符:string separator2(",\n");

    #include <iostream>
    #include <boost/tokenizer.hpp>
    #include <boost/algorithm/string.hpp>
    
    using namespace std;
    
       using namespace boost;
    
    int main() {
        string str = "TEst,hola\nhola";
        string separator1(""); //dont let quoted arguments escape themselves
        string separator2(",\n"); //split on comma and newline
        string separator3("\""); //let it have quoted arguments
    
        escaped_list_separator<char> els(separator1, separator2, separator3);
        tokenizer<escaped_list_separator<char>> tok(str, els);
    
        int counter = 0, current_siding = 0, wagon_pos = 0, cur_vector_pos = 0;
    
        string next;
    
        for (tokenizer<escaped_list_separator<char>>::iterator beg = tok.begin();     beg != tok.end(); ++beg) {
            next = *beg;
            boost::trim(next);
            cout << counter << " " << next << endl;
            counter++;
    
        }
        return 0;
    }  
    

    【讨论】:

    • 这是真的,但现在我有一个错误:候选人需要 3 个参数,提供 4 个
    • 是的,我也做了同样的事情,但是你没有收到那个错误 engine.cpp:43:84: error: no matching function for call to 'boost::escaped_list_separator::escaped_list_separator( std::string&, std::string&, std::string&, std::string&)' 候选者需要 3 个参数,提供 4 个
    • 对不起,我的错,在 separator2 中你可以使用包含一些字段分隔符的字符串
    • 我刚刚测试了您代码中的最后一个更改,并且编译正常。不幸的是输出没有改变 - 我仍然需要一个解决方案
    • 我们没有注意到的是我们离开了;//, els);已评论,但是当我取消评论时,在抛出 'std::length_error' what(): basic_string::_S_create 的实例后调用此错误终止
    【解决方案2】:

    在我看来你是在解析,而不是分裂。

    使用解析器生成器将是更好的 IMO

    Live On Coliru

    #include <boost/spirit/include/qi.hpp>
    namespace qi = boost::spirit::qi;
    
    int main() {
        boost::spirit::istream_iterator f(std::cin >> std::noskipws), l;
    
        std::vector<std::string> columns;
        qi::parse(f, l, +~qi::char_(",\r\n") % (qi::eol | ','), columns);
    
        size_t n = 0;
        for(auto& tok : columns) { std::cout << n++ << "\t" << tok << "\n"; }
    }
    

    打印

    0   12345
    1    Test Test
    2    Test
    3   98765
    4    Test2 test2
    5    Test2
    

    坦率地说,我认为它很棒,因为它可以让你写作

    phrase_parse(f, l, (qi::_int >> *(',' >> +~qi::char_("\r\n,")) % qi::eol, qi::blank...);
    

    并“免费”正确解析数据类型、跳过空格等

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 2015-09-18
      • 1970-01-01
      • 2011-02-08
      • 1970-01-01
      相关资源
      最近更新 更多