其他分享
首页 > 其他分享> > c – 从给定的Boost token_iterator中识别原始字符串中的位置

c – 从给定的Boost token_iterator中识别原始字符串中的位置

作者:互联网

如果使用Boost标记生成器处理了字符串,则可以获取给定标记迭代器指向的原始字符串中的位置:

boost:tokenizer<> tok( "this is the original string" );
for(tokenizer<>::iterator it=tok.begin(); it!=tok.end();++it)
{
    std::string strToken = *it;
    int charPos = it.?                /* IS THERE A METHOD? */
}

我意识到我可以使用已定义的’keep delimiters’列表创建一个特定的char_separator并指定keep_empty_tokens来尝试跟踪迭代器的进度,但我希望有一种更简单的方法只使用迭代器本身.

解决方法:

这似乎是您正在寻找的:

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

int main()
{
  typedef boost::tokenizer<> tok_t;

  std::string const s = "this is the original string";
  tok_t const tok(s);
  for (tok_t::const_iterator it = tok.begin(), it_end = tok.end(); it != it_end; ++it)
  {
    std::string::difference_type const offset = it.base() - s.begin() - it->size();
    std::cout << offset << "\t::\t" << *it << '\n';
  }
}

Online Demo

标签:c,tokenize,boost,stl
来源: https://codeday.me/bug/20190827/1736009.html