【问题标题】:boost split recursively and append to set递归提升拆分并附加到集合
【发布时间】:2014-06-12 09:56:02
【问题描述】:

我有一组用# 分隔的字符串。我想将它们拆分并插入 unordered_set。

例如。

abc#def#ghi xyz#mno#pqr

我通过传递无序集来使用提升拆分。但每次我得到新的结果集。我想将下一个结果追加到同一个集合中。

std::string str1 =  "abc#def#ghi";
std::string str2 = "xyz#mno#pqr";
std::unordered_set<std::string> result
boost::split(result, str1, boost::is_any_of("#"));
boost::split(result, str2, boost::is_any_of("#"));

如果我检查结果集,我只会得到 xyz、mno、pqr。我希望它附加了“abc def and ghi”。如何实现它。

注意:我不想使用任何额外的容器。

【问题讨论】:

    标签: c++ boost stl


    【解决方案1】:

    我会这样做:(看到它Live On Coliru

    #include <sstream>
    #include <unordered_set>
    #include <iostream>
    
    int main()
    {
        std::unordered_set<std::string> result;
    
        std::istringstream iss("abc#def#ghi");
    
        std::string tok;
        while (std::getline(iss, tok, '#'))
             result.insert(tok);
    
        iss.str("xyz#mno#pqr");
        iss.clear();
    
        while (std::getline(iss, tok, '#'))
             result.insert(tok);
    
        for (auto& s : result)
            std::cout << s << "\n";
    }
    

    【讨论】:

    • 这会比 boost::split 更快吗?
    【解决方案2】:

    这是因为boost::split 在写入目标容器之前清理了它。

    我会使用 boost::tokenizer 来满足您的需求。

    #include<boost/tokenizer>
    
    // ....
    
    typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
    boost::char_separator<char> sep("#");
    
    std::string str1 =  "abc#def#ghi";
    std::string str2 = "xyz#mno#pqr";
    
    std::unordered_set<std::string> result;
    
    tokenizer t1(str1, sep), t2(str2, sep);
    
    std::copy(t1.begin(), t1.end(), std::inserter(result, result.end()) );
    std::copy(t2.begin(), t2.end(), std::inserter(result, result.end()) );
    

    【讨论】:

      猜你喜欢
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      相关资源
      最近更新 更多