set与hash_set
原文:http://blog.csdn.net/morewindows/article/details/7029587
STL系列之六 set与hash_set
set和hash_set是STL中比较重要的容器,有必要对其进行深入了解。在STL中,set是以红黑树(RB-tree)作为底层数据结构的,hash_set是以Hash table(哈希表)作为底层数据结构的。set可以在时间复杂度为O(logN)情况下插入、删除和查找数据。hash_set操作的时间复杂度则比较复杂,这取决于哈希函数和哈希表的负载情况。下面列出set和hash_set的常用函数:
set和hase_set的更多函数请查阅MSDN。
set的使用范例如下(hash_set类似):
- // by MoreWindows( http://blog.csdn.net/MoreWindows )
- #include <set>
- #include <ctime>
- #include <cstdio>
- using namespace std;
- int main()
- {
- printf("--set使用 by MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");
- const int MAXN = 15;
- int a[MAXN];
- int i;
- srand(time(NULL));
- for (i = 0; i < MAXN; ++i)
- a[i] = rand() % (MAXN * 2);
- set<int> iset;
- set<int>::iterator pos;
- //插入数据 insert()有三种重载
- iset.insert(a, a + MAXN);
- //当前集合中个数 最大容纳数据量
- printf("当前集合中个数: %d 最大容纳数据量: %d\n", iset.size(), iset.max_size());
- //依次输出
- printf("依次输出集合中所有元素-------\n");
- for (pos = iset.begin(); pos != iset.end(); ++pos)
- printf("%d ", *pos);
- putchar('\n');
- //查找
- int findNum = MAXN;
- printf("查找 %d是否存在-----------------------\n", findNum);
- pos = iset.find(findNum);
- if (pos != iset.end())
- printf("%d 存在\n", findNum);
- else
- printf("%d 不存在\n", findNum);
- //在最后位置插入数据,如果给定的位置不正确,会重新找个正确的位置并返回该位置
- pos = iset.insert(--iset.end(), MAXN * 2);
- printf("已经插入%d\n", *pos);
- //删除
- iset.erase(MAXN);
- printf("已经删除%d\n", MAXN);
- //依次输出
- printf("依次输出集合中所有元素-------\n");
- for (pos = iset.begin(); pos != iset.end(); ++pos)
- printf("%d ", *pos);
- putchar('\n');
- return 0;
- }
运行结果如下:
set与hash_set的更多相关文章
- 深入了解STL中set与hash_set,hash表基础
一,set和hash_set简介 在STL中,set是以红黑树(RB-Tree)作为底层数据结构的,hash_set是以哈希表(Hash table)作为底层数据结构的.set可以在时间复杂度为O(l ...
- STL之hash_set和hash_map
Contents 1 hash_set和hash_map的创建与遍历 2 hash_set和hash_map的查找 3 建议 一句话hash_set和hash_map:它们皆由Hashtable(St ...
- #include <hash_set>
哈希查找,不需要排序,适用于精确查找,比二分查找更快 #define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS #include <iostream&g ...
- 【STL】关联容器 — hash_set
容器hash_set是以hash table为底层机制的,差点儿所有的操作都是转调用hash table提供的接口.因为插入无法存储同样的键值,所以hash_set的插入操作所有都使用hash tab ...
- set 和hash_set和海量数据的处理问题
什么样的结构决定其什么样的性质,因为set/map/multiset/multimap都是基于RB-tree之上,所以有自动排序功能, 而hash_set/hash_map/hash_multiset ...
- Linux包括hash_map和hash_set的not declared问题
当在Linux下cpp文件包括hash_map或hash_set时.会出现"'hash_map' was not declared in this scope"问题. #inclu ...
- 14.hash_set(已过时,被unorded_set替代)
#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS #include <iostream> #include <hash_set> ...
- hash_set和hash_map
1.hash_set集合容器 hash_set利用链式哈希表,进行数据的插入.删除和搜索.与set容器同样,不同意插入反复键值的元素.SGIC++哈希表是一个链式的结构,由表头和一系列单链组成.表头是 ...
- 17.查找效率最高的unorderd_set(替代hash_set)
#include <string> #include <iostream> //查询性能最高(不允许重复数据) #include <unordered_set> u ...
随机推荐
- Page Scroll Menu (页面中锚点菜单)
Technorati 标签: Page Scroll Menu,页面锚点菜单,Menu,Too Long,页面太长 当页面太长时,会导致浏览不便,这时就需要一个页面锚点菜单(Page Scroll ...
- java多线程之synchronized(线程同步)
一.线程同步,主要应用synchronized关键字: public class TraditionalThreadSynchorinzed { public static void main(Str ...
- 分区表,桶表,外部表,以及hive一些命令行小工具
hive中的表与hdfs中的文件通过metastore关联起来的.Hive的数据模型:内部表,分区表,外部表,桶表受控表(managed table):包括内部表,分区表,桶表 内部表: 我们删除表的 ...
- cc.RepeatForever和cc.Spawn冲突
正确 var tmpShip3 = cc.Sprite.createWithSpriteFrameName("w1_1.png"); tmpShip3.setPosition(,) ...
- bool?
public class GuestResponse { [Required(ErrorMessage = "Please enter your name")] public st ...
- c++builder XE8 线程 Thread
thread Thread c++builder XE8 / RAD 10 Settle delphi TThread.CreateAnonymousThread(MyMethod).Start; ...
- 2 weekend110的HDFS的JAVA客户端编写 + filesystem设计思想总结
HDFS的JAVA客户端编写 现在,我们来玩玩,在linux系统里,玩eclipse 或者, 即,更改图标,成功 这个,别慌.重新换个版本就好,有错误出错是好事. http://www.eclips ...
- vim显示历史命令
[vim显示历史命令] q: 进入命令历史编辑.类似的还有 q/ 可以进入搜索历史编辑.注意 q 后面如果跟随其它字母,是进入命令记录. 可以像编辑缓冲区一样编辑某个命令,然后回车执行.也可以用 ct ...
- POJ1384Piggy-Bank(DP)
POJ1384http://poj.org/problem?id=1384 最简单的完全背包问题,注意下初始化为INF就可以. #include <map> #include <se ...
- [Sparrow OS 设计文档连载(一)] Introduction