Using boost::tokenizer with string delimiters

前端 未结 4 1562
南笙
南笙 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: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 tok("some input...some other input");
    

    and use, like a usual boost tokenizer

提交回复
热议问题