set_intersection 可以与 C++ 中的 hash_set 一起使用吗?
我正在计算集合的交集、并集和差集。 我有一个我设置类型的 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 无缝协作吗?
I am calculating intersection, union and differences of sets.
I have a typedef of my set type:
typedef set<node_type> node_set;
When it is replaced with
typedef hash_set<node_type> node_set;
The results are different. It's a complicated program, and before I start debugging - am I doing it right? When I use functions like this:
set_intersection(v_higher.begin(), v_higher.end(), neighbors[w].begin(), neighbors[w].end(),
insert_iterator<node_set>(tmp1, tmp1.begin()));
- should they work seamlessly with both set and hash_set?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不这么认为。
set_intersection
的前提条件之一是:[first1, last1)
根据operator<
升序排列。也就是说,对于[first1, last1)
中的每对迭代器i
和j
,使得i
优先于 <代码>j,<代码>*j < *i 为假。hash_set
(和unordered_set
)是无序的,因此无法满足有序条件。有关如何与
unordered_set 相交的信息,请参阅 tr1::unordered_set 并集和交集
。I don't think so.
One of the pre-condition of
set_intersection
is:[first1, last1)
is ordered in ascending order according tooperator<
. That is, for every pair of iteratorsi
andj
in[first1, last1)
such thati
precedesj
,*j < *i
is false.The
hash_set
(andunordered_set
) is unordered, so the ordered condition cannot be satisfied.See tr1::unordered_set union and intersection on how to intersect
unordered_set
s.我打算不去。请记住,
hash_set
不是标准 C++,也永远不会是,它是不再受支持的旧扩展。较新的“哈希映射”称为unordered_set
和unordered_map
,可在 TR1、Boost 和 C++0x 中使用。之所以拒绝,是因为
set_intersection
要求对输入数据进行排序。相反,哈希映射如此快的原因是它放弃了排序。这在unordered_set
这个名称下显然更加明显。所以前提条件不能可靠地满足。I'm going to go with no. Keep in mind
hash_set
isn't standard C++ and never will be, it's an older extension that's no longer supported. The newer "hash maps" are calledunordered_set
andunordered_map
, available in TR1, Boost, and C++0x.The reason it's a no is that
set_intersection
requires the input data to be sorted. Contrarily, the reason a hash map is so quick is it gives up ordering. This is obviously more pronounced under the nameunordered_set
. So the precondition cannot be reliably met.不可以,您不能使用
set_intersection
,因为set_intersection
要求对两个集合进行排序(使用相同的顺序)。哈希集不以任何方式排序。在 C++0x 中,它们实际上被称为unordered_set
。No, you can't use
set_intersection
becauseset_intersection
requires that the two sets are ordered (using the same ordering). Hash sets aren't ordered in any way. In C++0x they will in fact be calledunordered_set
.