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 ...
随机推荐
- Eclipse配置信息
1.Eclipse VM arguments的保存位置: .metadata\.plugins\org.eclipse.debug.core\.launches (使用文件比较工具找出配置信息的保存位 ...
- 转】MyEclipse使用总结——MyEclipse中配置WebLogic12c服务器
原博文出自于:http://www.cnblogs.com/xdp-gacl/p/4142495.html 感谢! MyEclipse中配置WebLogic12c服务器的步骤如下: [Window]→ ...
- SpriteParticle II
[SpriteParticle II] 1.Randomizing the Starting Position 2.Setting the Initial Angle 3.Setting a Part ...
- Java反射机制(Class类的使用)
1:通过无参构造实例化对象 package cn.itcast; /* * 通过无参构造实例化对象 * 通过Class类本身实例化对象,使用newInstance方法 * 需要注意的是:实例化类中存在 ...
- pureMVC学习之一
//1var MainWindow: TMainWindow;begin Application.Initialize; Application.MainFormOnTaskbar := Tru ...
- <meta http-equiv = "X-UA-Compatible" cotent = "IE=edge,chrome=1"/>
<meta http-equiv = "X-UA-Compatible" cotent = "IE=edge,chrome=1"/> 制定ie调用哪 ...
- HTML第九天学习笔记
今天就继续看了下浮点float属性,代码如下: <html> <head> <title>CSS float属性</title> <meta ht ...
- PostgreSQL停止动作观察
实验过程如下: 启动一个客户端: [postgres@cnrd56 bin]$ ./psql psql () Type "help" for help. postgres=# be ...
- li中包含span,在IE6、IE7下会有3pxbug
如果给每个li里面加个span标签的话,在IE6,IE7下看,li与li之间的距离就会多了3px. 解决方法:在li中加vertical-align:middle; <div class=&qu ...
- C# 操作数据库就的那点代码
操作数据库的那点代码,别在费劲每个数据库都写一遍SQLHelper,SQLiteHelper,OleDbHelper,了,这里都有了. 接口不发了,自己抽取定义就行了. public abstract ...