我需要用之类的东西标记('','\n','\t'作为分隔符)文本
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
谢谢你的帮助。
我不会在这里使用古老的Tokenizer。只需使用字符串算法的split
产品:
在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(" nt"), 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'