/*
*
**************************************** **************************************** ****
* Basic description of the hash_set hash set container:
**************************************** **************************************** ****
*
* Hash_set hash collection container: an associated container with efficient data retrieval using the hashtable Data Structure
*
* Reverse iterators are not provided. Only the iterator and const_iterator of the forward iterator are provided.
* Repeated element key values cannot be inserted.
* Hashed associative container simple associative container unique associative container
*
* Currently, it is not a standard C ++ container, but an extension container of sgi c ++ STL.
* To use hash_set, you must use a macro statement # include *
**************************************** **************************************** ******
*
* Create a hash_set object:
* 1. hash_set <int> HS; // compare the key value with the default function object pai_to <value>
* 2. hash_set (size_type N); // In the prime number list, locate the first prime number greater than or equal to N as the table length: hash_set <int> HS (100 );
* 3. hash_set (size_type N, const hasher & H); // the hash function object is H.
* 4. hash_set (size_type N, const hasher & H, const key_equal & K); // key-Value Comparison function object K
* 5. hash_set (const hash_set & H); // use a hash set container to copy and generate another hash set container: hash_set <int> hs2 (HS );
*
**************************************** **************************************** ******
*
* Element insertion
* // Typedef pair <const key, T> value_type;
* Pair <iterator, bool> insert (const value_type & V); // Second: returns true/false indicating successful insertion.
* Void insert (iterator POs, const value_type & V );
*
**************************************** **************************************** ******
*
* Deletion of Elements
* Void erase (iterator POS );
* Size_type erase (const key_type & K); // delete an element equal to key value K
* Void erase (first, last); // deletes the elements in the [first, last) interval.
* Void clear ();
*
**************************************** **************************************** ******
*
* Access and search
*
* Iterator begin (); iterator end (); // The elements are not sorted and traversed.
*
* Iterator find (const key_type & K) const; // for non-default types such as char *, the related function object should be defined in the search.
*
* Other common functions
* Bool empty () const;
* Size_type size () const;
* Size_type bucket_count (const key_type & K) const; // obtain the table length of the hash table
* Void swap ();
* Resize ();
* Iterator lower_bound (); iterator upper_bound (); pair <iterator, iterator> interval _range (); // upper bound, next interval, and definite interval
*
* In sgi stl, the following hash functions are provided:
* Struct hash <char *>
* Struct hash <const char *>
* Struct hash <char>
* Struct hash <unsigned char>
* Struct hash <signed Char>
* Struct hash <short>
* Struct hash <unsigned short>
* Struct hash <int>
* Struct hash <unsigned int>
* Struct hash <long>
* Struct hash <unsigned long>
*
* The hash function determines how to scatter a list.
*
*
*
**************************************** ****
** Cumirror ** tongjinooo@163.com ****
**************************************** ****
*
*/
# Include # Include <iostream>
Struct student {
Char * Name;
Int age;
Char * city;
Char * phone;
};
// Comparison function of Custom Data
Class stuequal {
Public:
Bool operator () (const student & A, const student & B ){
Return strcmp (A. City, B. City) = 0; // The same name is not allowed, and name is the key value.
} // Change name to City
};
// Hash function for Custom Data
// Typedef unsigned int size_t;
Struct stu_hash {
Size_t operator () (const student & Stu) const
{
Unsigned long res = 0;
Char * s = Stu. City;
For (; * s; ++ s ){
Res = 5 * res + * s;
}
Return size_t (RES );
}
};
// Compare function objects for strings
Class strequal {
Public:
Bool operator () (const char * a, const char * B) const {
Return strcmp (a, B) = 0;
}
};
Int main (){
Using namespace STD;
Hash_set <const char *, hash <const char *>, strequal>;
A. insert ("tongjin ");
A. insert ("cumirror ");
A. insert ("makelaugh ");
A. insert ("feiguodeyun ");
// Hash <const char *> default hash function object
Hash_set <const char *, hash <const char *>, strequal >:: const_iterator B = A. Find ("tongjin ");
Cout <* B <"is" <(B! = A. End ()? "Present": "Not present") <Endl;
// For user-defined data, the hash function object and comparison function object should be constructed when the hash-related container is used.
// Differentiate the functions of hash function objects and comparison function objects
Student s [] = {
{"Tong Jin", 23, "Changsha", "XXX "},
{"Boss", 23, "Wuhan", "XXX "},
{"Dumplings", 23, "Fuzhou", "XXX "},
{"Wang tiao", 23, "Earth", "XXX "},
{"Zhou runfa", 23, "Hong Kong", "XXX "},
{"", 23, "Hong Kong", "XXX"}, // duplicate City
{"Tong Jin", 23, "Hong Kong", "XXX"} // duplicate names and duplicate cities
};
Hash_set <student, stu_hash, stuequal> C;
C. insert (s [0]);
C. insert (s [1]);
C. insert (s [2]);
C. insert (s [3]);
C. insert (s [4]);
C. insert (s [5]);
C. insert (s [6]);
// Note that the hash container cannot be sorted.
For (hash_set <student, stu_hash, stuequal >:: iterator I = C. Begin (); I! = C. End (); I ++ ){
Cout <I-> name <"" <I-> age <"" <I-> city <Endl;
}
Return 0;
}