Using boost::tokenizer with string delimiters

前端 未结 4 1539
南笙
南笙 2020-12-31 11:34

I\'ve been looking boost::tokenizer, and I\'ve found that the documentation is very thin. Is it possible to make it tokenize a string such as \"dolphin--monkey--baboon\" and

4条回答
  •  长发绾君心
    2020-12-31 12:22

    One option is to try boost::regex. Not sure of the performance compared to a custom tokenizer.

    std::string s = "dolphin--monkey--baboon";
    
    boost::regex re("[a-z|A-Z]+|--");
    boost::sregex_token_iterator iter(s.begin(), s.end() , re, 0);
    boost::sregex_token_iterator end_iter;
    
    while(iter != end_iter)
    {
        std::cout << *iter << '\n';
        ++iter;
    }
    

提交回复
热议问题