如何分割字符串,并使用boost ::分裂保持分隔符? [英] How to split a string and keep the delimiters using boost::split?
问题描述
我有一个这样的字符串:
I have a string like this:
std::string input("I #am going to# learn how #to use #boost# library#");
我这样做:
std::vector<std::string> splitVector;
boost::split(splitVector, input, boost::is_any_of("#"));
和得到这个:(splitVector)
And got this: (splitVector)
splitVector:
"I "
"am going to"
" learn how "
"to use "
"boos"
" library"
"" // **That's odd, why do I have an empty string here ?**
但需要的东西是这样的:
But need something like this:
splitVector:
"I "
"#am going to"
"# learn how "
"#to use "
"#boost"
"# library"
"#"
如何做到这一点?或者,也许有另一种方式来做到这一点升压库?
而为什么会在 splitVector
?
推荐答案
您不能使用的boost ::拆分
,因为使用内部实施 split_iterator
从升压/算法/串/ find_iterator.hpp
燕子令牌。
You cannot use boost::split
because the internal implementation that uses the split_iterator
from boost/algorithm/string/find_iterator.hpp
swallows the tokens.
但是你可以用的boost ::标记生成器
,度日,因为它有一个选项,以保持分隔符:
However you can get by with boost::tokenizer
, as it has an option to keep the delimiters:
每当一个定界符在输入序列中看到的那样,当前令牌完成时,一个新的标记开始。在dropped_delims的分隔符不显示在输出标记,而在kept_delims的分隔符不显示为令牌。结果<一个href=\"http://www.boost.org/doc/libs/1_55_0/libs/tokenizer/char_separator.htm\">http://www.boost.org/doc/libs/1_55_0/libs/tokenizer/char_separator.htm
Whenever a delimiter is seen in the input sequence, the current token is finished, and a new token begins. The delimiters in dropped_delims do not show up as tokens in the output whereas the delimiters in kept_delims do show up as tokens.
http://www.boost.org/doc/libs/1_55_0/libs/tokenizer/char_separator.htm
See next live:
#include <iostream>
#include <string>
#include <boost/tokenizer.hpp>
int main() {
// added consecutive tokens for illustration
std::string text = "I #am going to# learn how ####to use #boost# library#";
boost::char_separator<char> sep("", "#"); // specify only the kept separators
boost::tokenizer<boost::char_separator<char>> tokens(text, sep);
for (std::string t : tokens) { std::cout << "[" << t << "]" << std::endl; }
}
/* Output:
[I ]
[#]
[am going to]
[#]
[ learn how ]
[#]
[#]
[#]
[#]
[to use ]
[#]
[boost]
[#]
[ library]
[#] */
这篇关于如何分割字符串,并使用boost ::分裂保持分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!