的boost ::标记生成器点分开,还保持空字段 [英] Boost::tokenizer point seperated, but also keeping empty fields
问题描述
我已经看到这个问题和我很相似,但它是不同的,所以请不要标注它是重复的。
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 ::字符串s =This.is..a.test的字符串;
和我想要得到的字段<这家> <&是GT; <> < A> <试验方式>
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 << "> ";
}
但我得到&LT;这家&GT; &LT;&GT; &LT;&是GT; &LT;&GT; &LT;&GT; &LT;&GT; &LT; A&GT; &lt;试验方式&gt;
推荐答案
的第二个参数Boost.Tokenizer的<一个href=\"http://www.boost.org/doc/libs/1_55_0/libs/tokenizer/char_separator.htm\"><$c$c>char_separator$c$c>是 kept_delims
参数。它用于指定一个分隔符,将显示为令牌。原来的code为指定,
应保持作为标记。要解决此问题,更改:
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 ::标记生成器点分开,还保持空字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!