Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a Classifier with a split_iterator

Tags:

c++

boost

There is something about the boost string algorithms that I seem to be missing. I am trying to use a split_iterator with a using Clasifier as the split point. So, for example I would like to be able to do something like this:

make_split_iterator(str, is_space);

however make_split_iterator expects a Range and a Finder. So what I need is to find some sequence to create a Finder from a Classifier. Does anyone know how to do this, or if it is even possible?

like image 400
ltc Avatar asked Jan 21 '23 03:01

ltc


1 Answers

You can use token_finder, as in make_split_iterator(str, token_finder(is_space())) or make_split_iterator(str, token_finder(is_any_of(" "))) etc.

Full example, assuming your goal is tokenize (hence token_compress_on)

#include <string>
#include <iostream>
#include <boost/algorithm/string.hpp>
int main()
{
        std::string str = "This is a      test string";

        for( boost::algorithm::split_iterator<std::string::iterator> i
            = make_split_iterator(str, token_finder(
                                        boost::algorithm::is_space(),
                                        boost::algorithm::token_compress_on));
             i != boost::algorithm::split_iterator<std::string::iterator>();
             ++i)
        {
                std::cout << *i << '\n';
        }
}

test run: https://ideone.com/vQ2ZM

like image 106
Cubbi Avatar answered Jan 31 '23 19:01

Cubbi