2014-02-18 43 views
3

我有一个这样的字符串:如何分割字符串并使用boost :: split保持分隔符?

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" 
    "#" 

如何做到这一点?或者,也许有另一种方法来在推动库中做到这一点? 为什么我会在splitVector中得到空字符串?

+0

为什么你需要保持分隔符? – kaspersky

+0

@ gg.kaspersky,好问题!结果我必须恢复相同的字符串(使用splitVector来构建它),并且我有一个问题来检测字符串中有多少个分隔符,即奇数或偶数,换句话说,我总是将其恢复,因为它有偶数个分隔符。例如:如果我有字符串“#test”和“#test#”并分割它,请获取第一个字符串“test”,并将相同的第二个字符串“test”,并将这两个字符串还原为“#test#” –

+0

由于最后一个分隔符后面的输入字符串为空,因此存在空字符串。由于您的分隔符是单个字符,因此您可以简单地将该字符前缀(或附加)为结果字符串。如果有很多不同的分隔符,我不认为增强分割有你需要的功能。请参阅例如[this](http://stackoverflow.com/questions/1511029/tokenize-a-string-and-include-delimiters-in-c)其他解决方案的问题。 – user2079303

回答

5

不能使用boost::split,因为使用来自boost/algorithm/string/find_iterator.hppsplit_iterator的内部实现吞下了令牌。

但是你可以用boost::tokenizer获得通过,因为它有一个选项,以保持分隔符:

每当一个分隔符输入序列看出,目前令牌结束,一个新的令牌开始。 dropped_delims中的分隔符不会显示为输出中的标记,而retain_delims中的分隔符会显示为标记。
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] 
[#] */