Set and Hash_set

Source: Internet
Author: User

Original: http://blog.csdn.net/morewindows/article/details/7029587

The six set and Hash_set of STL series

Set and Hash_set are the more important containers in STL, and it is necessary to understand them deeply. In STL, set is a red black tree (Rb-tree) as the underlying data structure, Hash_set is a hash table (hash table) as the underlying data structure. Set can insert, delete, and find data in the case of a time complexity of O (Logn). The time complexity of the hash_set operation is more complex, depending on the hash function and the load situation of the hash table. The common functions of set and Hash_set are listed below:

For more functions of set and Hase_set, please refer to MSDN.

The use example of set is as follows (Hash_set):

[CPP]View Plaincopy
  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 using 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. //Inserting data Insert () There are three kinds of overloads
  18. Iset.insert (A, A + MAXN);
  19. ///The maximum number of data in the current collection
  20. printf ("Number of current collections:%d Maximum capacity:%d\n", Iset.size (), iset.max_size ());
  21. //output in sequence
  22. printf ("Output all elements of the collection in turn-------\ n");
  23. For (pos = Iset.begin (); pos! = Iset.end (); ++pos)
  24. printf ("%d", *pos);
  25. Putchar (' \ n ');
  26. //Find
  27. int findnum = MAXN;
  28. printf ("Find out if%d exists-----------------------\ n", FindNum);
  29. pos = Iset.find (FindNum);
  30. if (pos! = Iset.end ())
  31. printf ("%d exists \ n", FindNum);
  32. Else
  33. printf ("%d does not exist \ n", FindNum);
  34. //Insert the data at the last position, and if the given location is incorrect, find the correct location and return to that location
  35. pos = Iset.insert (--iset.end (), MAXN * 2);
  36. printf ("already inserted%d\n", *pos);
  37. //Delete
  38. Iset.erase (MAXN);
  39. printf ("deleted%d\n", MAXN);
  40. //output in sequence
  41. printf ("Output all elements of the collection in turn-------\ n");
  42. For (pos = Iset.begin (); pos! = Iset.end (); ++pos)
  43. printf ("%d", *pos);
  44. Putchar (' \ n ');
  45. return 0;
  46. }

The results of the operation are as follows:

Set and Hash_set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.