在C ++中,set

编程入门 行业动态 更新时间:2024-10-18 10:14:25
本文介绍了在C ++中,set_intersection可以和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无缝地工作,则

    <

解决方案

我不这么认为。

set_intersection 是:

  • [first1,last1) > 运算符< 按升序排序。也就是说,对于中的每对迭代器 i 和 j [first1,last1) / code>,使 i 在之前, * j < c $> ( unordered_set )是无序的,因此无法满足有序条件。

    请参阅 stackoverflow/questions/896155/c-tr1unordered-set-union-and-intersection 如何与 unordered_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?

    解决方案

    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 stackoverflow/questions/896155/c-tr1unordered-set-union-and-intersection on how to intersect unordered_sets.

更多推荐

在C ++中,set

本文发布于:2023-11-29 03:56:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1645193.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:set

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!