c++ - 递归地 boost 分割并附加到集合

标签 c++ boost stl

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

例如。

abc#def#ghi xyz#mno#pqr

我通过传递无序集来使用boost split。但每次我得到新的结果集。我想将下一个结果附加到同一组中。

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 和 ghi”。如何实现它。

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

最佳答案

我愿意:(参见 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";
}

关于c++ - 递归地 boost 分割并附加到集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24181487/

相关文章:

c++ - 为什么将 std::sort 与自定义比较器一起使用无法编译?

c++ - 在 Visual Studio 中引用静态库项目

c++ - printf 导致崩溃

c++ - boost RNG 的线程安全

c++ - 我怎样才能从 boost::random 得到 lognormal_distribution 来像 boost::math::lognormal_distribution

c++ - 调用在函数调用期间重新分配的 `std::vector` 元素的函数是否会引起麻烦?

c++ - std::binary_search 的自定义比较函数

c++ - 初始化器列表和运算符的 RHS

c++ - 为什么在调用隐式类型转换构造函数后直接是析构函数?

c++ - 当容器中的基本元素是 boost::tuple 时,为什么我不能使用 std::find?