我有一个像这样的字符串:
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)
splitVector:
"I "
"am going to"
" learn how "
"to use "
"boos"
" library"
"" // **That's odd, why do I have an empty string here ?**
但需要这样的东西:
splitVector:
"I "
"#am going to"
"# learn how "
"#to use "
"#boost"
"# library"
"#"
怎么做 ?或者也许在 boost 库中有另一种方法可以做到这一点?
为什么我会得到一个空字符串splitVector
?
你不能使用boost::split
因为内部实现使用split_iterator
from boost/algorithm/string/find_iterator.hpp
吞下代币。
不过你可以通过boost::tokenizer
,因为它可以选择保留分隔符:
每当在输入序列中看到分隔符时,当前标记就结束,并且新标记开始。 drop_delims 中的分隔符不会在输出中显示为标记,而 keep_delims 中的分隔符确实显示为标记。
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
下期直播见: http://coliru.stacked-crooked.com/a/6ee0726948cacb1c
#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]
[#] */
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)