我正在计算集合的交集、并集和差集。 我有一个我设置类型的 typedef:
typedef set<node_type> node_set;
当它被替换为
时typedef hash_set<node_type> node_set;
结果不同。这是一个复杂的程序,在我开始调试之前 - 我做得对吗?当我使用这样的函数时:
set_intersection(v_higher.begin(), v_higher.end(), neighbors[w].begin(), neighbors[w].end(),
insert_iterator<node_set>(tmp1, tmp1.begin()));
- 它们应该与 set 和 hash_set 无缝协作吗?
最佳答案
我不这么认为。
One of the pre-condition of set_intersection
是:
-
[first1, last1)
根据operator<
按升序排列 。也就是说,对于每对迭代器i
和j
在[first1, last1)
这样i
优先于j
,*j < *i
是假的。
hash_set
(和 unordered_set
)是无序的,因此无法满足有序条件。
参见tr1::unordered_set union and intersection关于如何相交 unordered_set
s。
关于c++ - C++中set_intersection可以和hash_set一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2435497/