在C ++中,set_intersection可以和hash_set一起使用吗? [英] Can set_intersection be used with hash_set in C++?

查看:120
本文介绍了在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 )是无序的,因此无法满足有序条件。



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

    这篇关于在C ++中,set_intersection可以和hash_set一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆