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
- by Morewindows (http://blog.csdn.net/MoreWindows)
- #include <set>
- #include <ctime>
- #include <cstdio>
- Using namespace std;
- int main ()
- {
- printf ("--set using 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;
- //Inserting data Insert () There are three kinds of overloads
- Iset.insert (A, A + MAXN);
- ///The maximum number of data in the current collection
- printf ("Number of current collections:%d Maximum capacity:%d\n", Iset.size (), iset.max_size ());
- //output in sequence
- printf ("Output all elements of the collection in turn-------\ n");
- For (pos = Iset.begin (); pos! = Iset.end (); ++pos)
- printf ("%d", *pos);
- Putchar (' \ n ');
- //Find
- int findnum = MAXN;
- printf ("Find out if%d exists-----------------------\ n", FindNum);
- pos = Iset.find (FindNum);
- if (pos! = Iset.end ())
- printf ("%d exists \ n", FindNum);
- Else
- printf ("%d does not exist \ n", FindNum);
- //Insert the data at the last position, and if the given location is incorrect, find the correct location and return to that location
- pos = Iset.insert (--iset.end (), MAXN * 2);
- printf ("already inserted%d\n", *pos);
- //Delete
- Iset.erase (MAXN);
- printf ("deleted%d\n", MAXN);
- //output in sequence
- printf ("Output all elements of the collection in turn-------\ n");
- For (pos = Iset.begin (); pos! = Iset.end (); ++pos)
- printf ("%d", *pos);
- Putchar (' \ n ');
- return 0;
- }
The results of the operation are as follows:
Set and Hash_set