set_intersection可以和C++中的hash_set一起使用吗?

R S*_*R S 2 c++ stl intersection set hashset

我正在计算集合的交集,并集和差异.我有一个我的set类型的typedef:

typedef set<node_type> node_set;
Run Code Online (Sandbox Code Playgroud)

当它被替换为

typedef hash_set<node_type> node_set;
Run Code Online (Sandbox Code Playgroud)

结果不同.这是一个复杂的程序,在我开始调试之前 - 我做得对吗?当我使用这样的函数时:

set_intersection(v_higher.begin(), v_higher.end(), neighbors[w].begin(), neighbors[w].end(), 
            insert_iterator<node_set>(tmp1, tmp1.begin()));
Run Code Online (Sandbox Code Playgroud)
  • 它们应该与set和hash_set无缝协作吗?

ken*_*ytm 5

我不这么认为.

其中一个先决条件set_intersection是:

  • [first1, last1)按升序排列根据operator<.也就是说,每对迭代器i,并j[first1, last1)这样i先于j,*j < *i是假的.

hash_set(和unordered_set)是无序的,所以有序的条件不能满足.

有关如何与s 相交,请参阅tr1 :: unordered_set union和intersectionunordered_set.