C++ 标记器点分隔,但也保留空字段
我看过,我的和它非常相似,但它是不同的,所以请不要标记为重复 我的问题是:如何从字符串中获取空字段 我有一个类似于C++ 标记器点分隔,但也保留空字段,c++,boost,tokenize,C++,Boost,Tokenize,我看过,我的和它非常相似,但它是不同的,所以请不要标记为重复 我的问题是:如何从字符串中获取空字段 我有一个类似于std::string s=“This.is..a.test”的字符串我想得到字段 我也试过了 typedef boost::char_separator<char> ChSep; typedef boost::tokenizer<ChSep> TknChSep; ChSep sep(".", ".", boost::keep_empty_tokens); T
std::string s=“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 << "> ";
}
typedef boost::char_分隔符ChSep;
typedef boost::标记器TknChSep;
ChSep sep(“.”,“”,boost::保留\u空\u令牌);
TknChSep-tok(s,sep);
for(TknChSep::迭代器beg=tok.begin();beg!=tok.end();++beg)
{
std::cout我想我应该跳过Boost::tokenizer
,只需使用标准正则表达式进行拆分:
#include <iterator>
#include <regex>
#include <string>
#include <iostream>
int main() {
std::string s = "This.is..a.test";
std::regex sep{ "\\." };
std::copy(std::sregex_token_iterator(s.begin(), s.end(), sep, -1),
std::sregex_token_iterator(),
std::ostream_iterator<std::string>(std::cout, "\n"));
}
Boost.Tokenizer的第二个参数是keeped_delims
参数。它用于指定将显示为令牌的分隔符。原始代码指定应将“
保留为令牌。若要解决此问题,请更改:
ChSep sep(".", ".", boost::keep_empty_tokens);
致:
下面是一个完整的示例:
#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;
}
#包括
#包括
#包括
#包括
int main()
{
std::string str=“This.is..a.test”;
typedef boost::标记器标记器;
boost::字符分隔符sep(
“,//删除的分隔符
“”,//保留分隔符
boost::keep_empty_tokens);//empty token策略
BOOST_FOREACH(std::字符串标记,标记器(str,sep))
{
标准::cout
ChSep sep(".", "", boost::keep_empty_tokens);
// ^-- no delimiters will show up as tokens.
#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;
}