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)
我不这么认为.
[first1, last1)是按升序排列根据operator<.也就是说,每对迭代器i,并j在[first1, last1)这样i先于j,*j < *i是假的.在hash_set(和unordered_set)是无序的,所以有序的条件不能满足.
有关如何与s 相交,请参阅tr1 :: unordered_set union和intersectionunordered_set.