Using boost::tokenizer with string delimiters

前端 未结 4 1446
南笙
南笙 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:05

    It looks like you will need to write your own TokenizerFunction to do what you want.

    0 讨论(0)
  • 2020-12-31 12:06

    I know the theme is quite old, but it is shown in the top links in google when I search "boost tokenizer by string"

    so I will add my variant of TokenizerFunction, just in case:

    class FindStrTFunc
    {
    public:
        FindStrTFunc() : m_str(g_dataSeparator)
        {
        }
    
        bool operator()(std::string::const_iterator& next,
            const std::string::const_iterator& end, std::string& tok) const
        {
            if (next == end)
            {
                return false;
            }
            const std::string::const_iterator foundToken =
                std::search(next, end, m_str.begin(), m_str.end());
            tok.assign(next, foundToken);
            next = (foundToken == end) ? end : foundToken + m_str.size();
            return true;
        }
    
        void reset()
        {
        }
    
    private:
        std::string m_str;
    };
    

    after we can create

    boost::tokenizer<FindStrTFunc> tok("some input...some other input");
    

    and use, like a usual boost tokenizer

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-12-31 12:24

    using iter_split allows you to use multiple character tokens. The code below would produce the following:

    dolphin
    mon-key
    baboon

    #include <iostream>
    #include <boost/foreach.hpp>
    #include <boost/algorithm/string.hpp>
    #include <boost/algorithm/string/iter_find.hpp>
    
        // code starts here
        std::string s = "dolphin--mon-key--baboon";
        std::list<std::string> stringList;
        boost::iter_split(stringList, s, boost::first_finder("--"));
    
        BOOST_FOREACH(std::string token, stringList)
        {    
            std::cout << token << '\n';  ;
        }
    
    0 讨论(0)
提交回复
热议问题