簡體   English   中英

可以在C ++中將set_intersection與hash_set一起使用嗎?

[英]Can set_intersection be used with hash_set in C++?

我正在計算交集,並集和集合的差異。 我有我的集合類型的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)根據operator< 升序排列 也就是說,對於[first1, last1)每對迭代器ij ,使得ij之前, *j < *i為假。

hash_set (和unordered_set )是無序的,因此無法滿足有序的條件。

有關如何與unordered_set 相交的信息 ,請參見tr1 :: unordered_set並集和交集

我要走了。 請記住, hash_set不是標准的C ++,而且永遠不會,它是不再支持的較舊擴展。 較新的“哈希映射”稱為unordered_setunordered_map ,可在TR1,Boost和C ++ 0x中使用。

否定的原因是set_intersection需要對輸入數據進行排序。 相反,哈希映射表之所以如此之快是因為它放棄了排序。 這顯然在unordered_set名稱下更加明顯。 因此,前提條件不能可靠地滿足。

不,您不能使用set_intersection因為set_intersection要求set_intersection兩個集合進行排序(使用相同的排序)。 哈希集不以任何方式排序。 實際上,在C ++ 0x中,它們將被稱為unordered_set

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM