[英]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()));
我不這么認為。
[first1, last1)
根據operator<
升序排列 。 也就是說,對於[first1, last1)
每對迭代器i
和j
,使得i
在j
之前, *j < *i
為假。 hash_set
(和unordered_set
)是無序的,因此無法滿足有序的條件。
有關如何與unordered_set
相交的信息 ,請參見tr1 :: unordered_set並集和交集 。
我要走了。 請記住, hash_set
不是標准的C ++,而且永遠不會,它是不再支持的較舊擴展。 較新的“哈希映射”稱為unordered_set
和unordered_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.