本文介绍了结合使用escaped_list_separator和boost split的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用boost字符串库,并且刚刚遇到了split方法的简单性.

I am playing around with the boost strings library and have just come across the awesome simplicity of the split method.

  string delimiters = ",";
  string str = "string, with, comma, delimited, tokens, \"and delimiters, inside a quote\"";
  // If we didn't care about delimiter characters within a quoted section we could us
  vector<string> tokens;  
  boost::split(tokens, str, boost::is_any_of(delimiters));
  // gives the wrong result: tokens = {"string", " with", " comma", " delimited", " tokens", "\"and delimiters", " inside a quote\""}

哪一个好而简洁...但是它似乎不适合使用引号,而是我必须做类似以下的事情

Which would be nice and concise... however it doesn't seem to work with quotes and instead I have to do something like the following

string delimiters = ",";
string str = "string, with, comma, delimited, tokens, \"and delimiters, inside a quote\"";
vector<string> tokens; 
escaped_list_separator<char> separator("\\",delimiters, "\"");
typedef tokenizer<escaped_list_separator<char> > Tokeniser;
Tokeniser t(str, separator);
for (Tokeniser::iterator it = t.begin(); it != t.end(); ++it)
    tokens.push_back(*it);
// gives the correct result: tokens = {"string", " with", " comma", " delimited", " tokens", "\"and delimiters, inside a quote\""}

我的问题是引用分隔符时可以拆分还是使用其他标准算法?多亏了purpledog,但是我已经有了不折不扣的实现预期结果的方法,我只是认为这很麻烦,除非我可以用更简单,更优雅的解决方案代替它,否则通常在不先将其包装进去之前就不会使用它.还有另一种方法.

My question is can split or another standard algorithm be used when you have quoted delimiters? Thanks to purpledog but I already have a non-deprecated way of achieving the desired outcome, I just think that it's quite cumbersome and unless I could replace it with a simpler more elegant solution I wouldn't use it in general without first wrapping it in yet another method.

更新了代码以显示结果并阐明问题.

Updated code to show results and clarify question.

推荐答案

似乎没有任何简单的方法可以使用boost :: split方法来执行此操作.我能找到的最短的代码是

It doesn't seem that there is any simple way to do this using the boost::split method. The shortest piece of code I can find to do this is

vector<string> tokens; 
tokenizer<escaped_list_separator<char> > t(str, escaped_list_separator<char>("\\", ",", "\""));
BOOST_FOREACH(string s, escTokeniser)
    tokens.push_back(s);  

仅比原始代码片段冗长

vector<string> tokens;  
boost::split(tokens, str, boost::is_any_of(","));

这篇关于结合使用escaped_list_separator和boost split的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 00:24