unordered_set VS set
2015-09-26 11:10
337 查看
在stackoverflow上看到一个问题,大概是问为什么有了平均查询O(1)的unordered_set我们还需要平均O(logN)的set呢?
首先我们需要知道set在STL底层是用红黑树来实现的,而unordered_set是基于hash。
在这里记录一下比较好的答案。
set uses less memory than unordered_set to store the same number of elements.(set更节省空间)
For a small number of elements, lookups in a set might be faster than lookups in an unordered_set.(小规模的话set或许更快)Even though many operations are faster in the average case for unordered_set, they are often guaranteed to have better worst case complexities for set (for example insert).(但总的来说基于hash的unordered_set还是快些)
That set sorts the elements is useful if you want to access them in order.
You can lexicographically compare different sets with <, <=, > and >=. unordered_sets are not required to support these operations.(set有序输出)
People tend to overlook the fact that hashtables have O(1) average-case access time, meaning they can occasionally have big delays. The distinction can be important for real-time systems.
(这一点也很重要,对于实时系统来说tree的最糟糕的情况还是比hash好)
首先我们需要知道set在STL底层是用红黑树来实现的,而unordered_set是基于hash。
在这里记录一下比较好的答案。
set uses less memory than unordered_set to store the same number of elements.(set更节省空间)
For a small number of elements, lookups in a set might be faster than lookups in an unordered_set.(小规模的话set或许更快)Even though many operations are faster in the average case for unordered_set, they are often guaranteed to have better worst case complexities for set (for example insert).(但总的来说基于hash的unordered_set还是快些)
That set sorts the elements is useful if you want to access them in order.
You can lexicographically compare different sets with <, <=, > and >=. unordered_sets are not required to support these operations.(set有序输出)
People tend to overlook the fact that hashtables have O(1) average-case access time, meaning they can occasionally have big delays. The distinction can be important for real-time systems.
(这一点也很重要,对于实时系统来说tree的最糟糕的情况还是比hash好)
相关文章推荐
- 我的java代码
- SpringMVC 结合HttpClient调用第三方接口实现
- 大道至简-第二章 心得体会
- 基于树莓派的四轴[仅基本功能]
- PAT(甲级)1063
- angularjs学习笔记之三大模块(modal,controller,view)
- 文章标题
- 黑马程序员————逼近法在java编程中的运用,开根号原理、已知x的x次幂的值求x
- C++文件中读单词并统计输出改单词及其个数
- 亿级Web系统搭建:单机到分布式集群
- HD1003(Max Sum)
- Lambda表达式和表达式树
- Settings
- PAT(甲级)1062
- tomcat重启脚本
- java 代码中实现 TextView的 DrawableTop属性
- 《数据结构》——排序
- Sping管理事务实现方式
- VB6基本数据库应用(十):【增补篇】游标Cursor和锁Lock简介
- 数据库之索引