c++ - C++中set_intersection可以和hash_set一起使用吗?

标签 c++ stl set intersection hashset

我正在计算集合的交集、并集和差集。 我有一个我设置类型的 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 无缝协作吗?

最佳答案

我不这么认为。

One of the pre-condition of set_intersection 是:

  • [first1, last1)根据operator<按升序排列 。也就是说,对于每对迭代器 ij[first1, last1)这样i优先于j , *j < *i是假的。

hash_set (和 unordered_set )是无序的,因此无法满足有序条件。

参见tr1::unordered_set union and intersection关于如何相交 unordered_set s。

关于c++ - C++中set_intersection可以和hash_set一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2435497/

相关文章:

python - 比较 Python 字典中保存的集合

c++ - 使用 lambda 创建 unordered_set

c++ - Microsoft Visual C++ Express 中是否提供创建工作区的选项?

c++ - 模板函数中的 std::function

c++ - std::map 发现在 C++ 中不起作用

database - 哪种 Erlang 数据结构可用于有序集并且可以进行查找?

c++ - 到底什么是潮红?

python clang : Getting Template Arguments

c++ - 创建 boost::tuple<std::string, std::string, int> 和 std::vector<int> 的映射

Python,mysql.connector 错误 : No Result Set to Fetch From; cgitb shows correct value being passed to function