原文: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类似):

  1. // by MoreWindows( http://blog.csdn.net/MoreWindows )
  2. #include <set>
  3. #include <ctime>
  4. #include <cstdio>
  5. using namespace std;
  6. int main()
  7. {
  8. printf("--set使用 by MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");
  9. const int MAXN = 15;
  10. int a[MAXN];
  11. int i;
  12. srand(time(NULL));
  13. for (i = 0; i < MAXN; ++i)
  14. a[i] = rand() % (MAXN * 2);
  15. set<int> iset;
  16. set<int>::iterator pos;
  17. //插入数据 insert()有三种重载
  18. iset.insert(a, a + MAXN);
  19. //当前集合中个数 最大容纳数据量
  20. printf("当前集合中个数: %d     最大容纳数据量: %d\n", iset.size(), iset.max_size());
  21. //依次输出
  22. printf("依次输出集合中所有元素-------\n");
  23. for (pos = iset.begin(); pos != iset.end(); ++pos)
  24. printf("%d ", *pos);
  25. putchar('\n');
  26. //查找
  27. int findNum = MAXN;
  28. printf("查找 %d是否存在-----------------------\n", findNum);
  29. pos = iset.find(findNum);
  30. if (pos != iset.end())
  31. printf("%d 存在\n", findNum);
  32. else
  33. printf("%d 不存在\n", findNum);
  34. //在最后位置插入数据,如果给定的位置不正确,会重新找个正确的位置并返回该位置
  35. pos  = iset.insert(--iset.end(), MAXN * 2);
  36. printf("已经插入%d\n", *pos);
  37. //删除
  38. iset.erase(MAXN);
  39. printf("已经删除%d\n", MAXN);
  40. //依次输出
  41. printf("依次输出集合中所有元素-------\n");
  42. for (pos = iset.begin(); pos != iset.end(); ++pos)
  43. printf("%d ", *pos);
  44. putchar('\n');
  45. return 0;
  46. }

运行结果如下:

set与hash_set的更多相关文章

  1. 深入了解STL中set与hash_set,hash表基础

    一,set和hash_set简介 在STL中,set是以红黑树(RB-Tree)作为底层数据结构的,hash_set是以哈希表(Hash table)作为底层数据结构的.set可以在时间复杂度为O(l ...

  2. STL之hash_set和hash_map

    Contents 1 hash_set和hash_map的创建与遍历 2 hash_set和hash_map的查找 3 建议 一句话hash_set和hash_map:它们皆由Hashtable(St ...

  3. #include <hash_set>

    哈希查找,不需要排序,适用于精确查找,比二分查找更快 #define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS #include <iostream&g ...

  4. 【STL】关联容器 — hash_set

    容器hash_set是以hash table为底层机制的,差点儿所有的操作都是转调用hash table提供的接口.因为插入无法存储同样的键值,所以hash_set的插入操作所有都使用hash tab ...

  5. set 和hash_set和海量数据的处理问题

    什么样的结构决定其什么样的性质,因为set/map/multiset/multimap都是基于RB-tree之上,所以有自动排序功能, 而hash_set/hash_map/hash_multiset ...

  6. Linux包括hash_map和hash_set的not declared问题

    当在Linux下cpp文件包括hash_map或hash_set时.会出现"'hash_map' was not declared in this scope"问题. #inclu ...

  7. 14.hash_set(已过时,被unorded_set替代)

    #define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS #include <iostream> #include <hash_set> ...

  8. hash_set和hash_map

    1.hash_set集合容器 hash_set利用链式哈希表,进行数据的插入.删除和搜索.与set容器同样,不同意插入反复键值的元素.SGIC++哈希表是一个链式的结构,由表头和一系列单链组成.表头是 ...

  9. 17.查找效率最高的unorderd_set(替代hash_set)

    #include <string> #include <iostream> //查询性能最高(不允许重复数据) #include <unordered_set> u ...

随机推荐

  1. Eclipse配置信息

    1.Eclipse VM arguments的保存位置: .metadata\.plugins\org.eclipse.debug.core\.launches (使用文件比较工具找出配置信息的保存位 ...

  2. 转】MyEclipse使用总结——MyEclipse中配置WebLogic12c服务器

    原博文出自于:http://www.cnblogs.com/xdp-gacl/p/4142495.html 感谢! MyEclipse中配置WebLogic12c服务器的步骤如下: [Window]→ ...

  3. SpriteParticle II

    [SpriteParticle II] 1.Randomizing the Starting Position 2.Setting the Initial Angle 3.Setting a Part ...

  4. Java反射机制(Class类的使用)

    1:通过无参构造实例化对象 package cn.itcast; /* * 通过无参构造实例化对象 * 通过Class类本身实例化对象,使用newInstance方法 * 需要注意的是:实例化类中存在 ...

  5. pureMVC学习之一

    //1var  MainWindow: TMainWindow;begin  Application.Initialize;  Application.MainFormOnTaskbar := Tru ...

  6. <meta http-equiv = "X-UA-Compatible" cotent = "IE=edge,chrome=1"/>

    <meta http-equiv = "X-UA-Compatible" cotent = "IE=edge,chrome=1"/> 制定ie调用哪 ...

  7. HTML第九天学习笔记

    今天就继续看了下浮点float属性,代码如下: <html> <head> <title>CSS float属性</title> <meta ht ...

  8. PostgreSQL停止动作观察

    实验过程如下: 启动一个客户端: [postgres@cnrd56 bin]$ ./psql psql () Type "help" for help. postgres=# be ...

  9. li中包含span,在IE6、IE7下会有3pxbug

    如果给每个li里面加个span标签的话,在IE6,IE7下看,li与li之间的距离就会多了3px. 解决方法:在li中加vertical-align:middle; <div class=&qu ...

  10. C# 操作数据库就的那点代码

    操作数据库的那点代码,别在费劲每个数据库都写一遍SQLHelper,SQLiteHelper,OleDbHelper,了,这里都有了. 接口不发了,自己抽取定义就行了. public abstract ...