如何在c++中找到两个std::hash_set的交集

How to find the intersection of two std::hash_set in C++?

本文关键字:hash std 两个 set c++      更新时间:2023-10-16

STD库是否提供任何函数允许我找到两个STD::hash_set的交集?

我尝试使用std::set_intersection,但我得到断言错误与Expression: sequence not ordered,有任何工作围绕这一点?

#include <hash_set>
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
int main()
{
    std::hash_set<std::string> h1;
    h1.insert("NoMainDraw");
    h1.insert("NoLight");
    h1.insert("Random");
    std::hash_set<std::string> h2;
    h2.insert("NoLight");
    std::vector<std::string> groupDifference;
    std::set_intersection(h1.cbegin(), h1.cend(),
                          h2.cbegin(), h2.cend(),
                          std::back_inserter(groupDifference));
    for(const std::string& i : groupDifference)
    {
        std::cout << i << std::endl;
    }
}

你必须迭代一个map做查找在另一个。没有标准库算法