1

我需要标记 (' ','\n','\t' 作为分隔符) 与 somethink like

std::string text = "foo   bar";
boost::iterator_range<std::string::iterator> r = some_func_i_dont_know(text);

后来我想得到输出:

for (auto i: result)
    std::cout << "distance: " << std::distance(text.begin(), i.begin())
        << "\nvalue: " << i << '\n';

上面的例子产生了什么:

distance: 0
value: foo
distance: 6
value: bar

谢谢你的帮助。

4

1 回答 1

2

我不会在这里使用古老的 Tokenizer。只需使用字符串算法的split产品:

Live On Coliru

#include <boost/algorithm/string.hpp>
#include <iostream>

using namespace boost;

int main()
{
    std::string text = "foo   bar";
    boost::iterator_range<std::string::iterator> r(text.begin(), text.end());

    std::vector<iterator_range<std::string::const_iterator> > result;
    algorithm::split(result, r, is_any_of(" \n\t"), algorithm::token_compress_on);

    for (auto i : result)
        std::cout << "distance: " << distance(text.cbegin(), i.begin()) << ", "
                  << "length: " << i.size() << ", "
                  << "value: '" << i << "'\n";
}

印刷

distance: 0, length: 3, value: 'foo'
distance: 6, length: 3, value: 'bar'
于 2014-10-15T20:12:52.240 回答