如何使我的分割仅在一根实线上工作并且能够跳过字符串的引用部分?

2024-01-31

所以我们有一个简单拆分 https://stackoverflow.com/questions/236129/how-to-split-a-string-in-c/236180#236180:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;

vector<string> split(const string& s, const string& delim, const bool keep_empty = true) {
    vector<string> result;
    if (delim.empty()) {
        result.push_back(s);
        return result;
    }
    string::const_iterator substart = s.begin(), subend;
    while (true) {
        subend = search(substart, s.end(), delim.begin(), delim.end());
        string temp(substart, subend);
        if (keep_empty || !temp.empty()) {
            result.push_back(temp);
        }
        if (subend == s.end()) {
            break;
        }
        substart = subend + delim.size();
    }
    return result;
}

or 升压分裂 http://www.boost.org/doc/libs/1_41_0/doc/html/string_algo/usage.html#id1701774。我们有简单的主要内容,例如:

int main() {
    const vector<string> words = split("close no \"\n matter\" how \n far", " ");
    copy(words.begin(), words.end(), ostream_iterator<string>(cout, "\n"));
}

如何让它输出类似的东西

close 
no
"\n matter"
how
end symbol found.

我们想介绍 splitstructures应保持不分割,并且字符应结束解析过程。怎么做这样的事情?


Updated为了表达对奖金的‘感谢’,我实现了 4 个功能,我最初将这些功能作为“你不会需要它”而跳过。

  1. 现在支持部分引用的列

    This is the problem you reported: e.g. with a delimiter , only test,"one,two",three would be valid, not test,one","two","three. Now both are accepted

  2. 现在支持自定义分隔符表达式

    You could only specify single characters as delimiters. Now you can specify any Spirit Qi parser expression as the delimiter rule. E.g

      splitInto(input, output, ' ');             // single space
      splitInto(input, output, +qi.lit(' '));    // one or more spaces
      splitInto(input, output, +qi.lit(" \t"));  // one or more spaces or tabs
      splitInto(input, output, (qi::double_ >> !'#') // -- any parse expression
    

    Note这改变了默认重载的行为

    The old version treated repeated spaces as a single delimiter by default. You now have to explicitly specify that (2nd example) if you want it.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使我的分割仅在一根实线上工作并且能够跳过字符串的引用部分? 的相关文章

随机推荐