Boost :: tokenizer点分开,但也保留空白字段 [英] Boost::tokenizer point separated, but also keeping empty fields

查看:175
本文介绍了Boost :: tokenizer点分开,但也保留空白字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了这个问题,而我的问题与之非常相似,但是有所不同,因此请不要标记它是重复的.

I have seen this question and mine is very similar to it, but it is different, so please do not mark it as duplicate.

我的问题是:如何从字符串中获取空白字段?

My question is: How do I get the empty fields from a string?

我有一个像std::string s = "This.is..a.test";这样的字符串,我想获取字段<This> <is> <> <a> <test>.

I have a string like std::string s = "This.is..a.test"; and I want to get the fields <This> <is> <> <a> <test>.

我也尝试过

typedef boost::char_separator<char> ChSep;
typedef boost::tokenizer<ChSep> TknChSep;
ChSep sep(".", ".", boost::keep_empty_tokens);
TknChSep tok(s, sep);
for (TknChSep::iterator beg = tok.begin(); beg != tok.end(); ++beg)
{
  std::cout << "<" << *beg << "> ";
}

但是我得到了<This> <.> <is> <.> <> <.> <a> <test>.

推荐答案

Boost.Tokenizer的应该作为令牌保留.要解决此问题,请更改:

The second argument to Boost.Tokenizer's char_separator is the kept_delims parameter. It is used to specify a delimiters that will show up as tokens. The original code is specifying that "." should be kept as a token. To resolve this, change:

ChSep sep(".", ".", boost::keep_empty_tokens);

收件人:

ChSep sep(".", "", boost::keep_empty_tokens);
            // ^-- no delimiters will show up as tokens.


这是一个完整的示例:


Here is a complete example:

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

int main()
{
  std::string str = "This.is..a.test";
  typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
  boost::char_separator<char> sep(
      ".", // dropped delimiters
      "",  // kept delimiters
      boost::keep_empty_tokens); // empty token policy

  BOOST_FOREACH(std::string token, tokenizer(str, sep))
  {
    std::cout << "<" << token << "> ";
  }
  std::cout << std::endl;
}

哪个会产生所需的输出:

Which produces the desired output:

<This> <is> <> <a> <test> 

这篇关于Boost :: tokenizer点分开,但也保留空白字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆