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

发布于 2024-08-24 22:23:02 字数 482 浏览 1 评论 0原文

我正在计算集合的交集、并集和差集。 我有一个我设置类型的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安稳善良 2024-08-31 22:23:02

我不这么认为。

set_intersection 的前提条件之一是:

  • [first1, last1) 根据operator<升序排列。也就是说,对于 [first1, last1) 中的每对迭代器 ij,使得 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 to operator<. That is, for every pair of iterators i and j in [first1, last1) such that i precedes j, *j < *i is false.

The hash_set (and unordered_set) is unordered, so the ordered condition cannot be satisfied.

See tr1::unordered_set union and intersection on how to intersect unordered_sets.

美人如玉 2024-08-31 22:23:02

我打算不去。请记住,hash_set 不是标准 C++,也永远不会是,它是不再受支持的旧扩展。较新的“哈希映射”称为 unordered_setunordered_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 called unordered_set and unordered_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 name unordered_set. So the precondition cannot be reliably met.

苍白女子 2024-08-31 22:23:02

不可以,您不能使用 set_intersection,因为 set_intersection 要求对两个集合进行排序(使用相同的顺序)。哈希集不以任何方式排序。在 C++0x 中,它们实际上被称为 unordered_set

No, you can't use set_intersection because set_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 called unordered_set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文