Vectors, Hashtable, HashSet and Hashtable

Source: Internet
Author: User

Description: The so-called advantages and disadvantages are only counted as a delimiter, in fact, only the use of different scenes, there is no good or inferior.

Vector

    The bottom of the "implementation" vector is an array, so that it can have an O (1) lookup efficiency. But it is long, the capacity to reach end_of_storage when the additional space to automatically expand one time, and the original data are copied to the new space, and then the original space destroyed. Due to the characteristics of its array, the efficiency of vector insertion and deletion is lower.    The "benefits" lookup is highly efficient.    "Disadvantage" in addition to finding, the other efficiency is not particularly high.    "Other" 1, in terms of advantages and disadvantages, the list and the queue are simply complementary to the vector, so in the appropriate application scenarios to choose the appropriate container; 2,vector's iterator not only overloads the ++,--, but also overloads the * (former and = =, this overload is more to consider the diversity of vector storage objects, to facilitate the use of custom objects.  Hashtable:    "Implementation" of C + + hashtable and ordinary Hashtable is no different, in the prevention of conflict and the use of zipper method. Its underlying implementation is vector+linkedlist, each bucket is a vector,vector is stored in the LinkedList, but the linkedlist here is not a list or slist in the STL,        It is the hash table node that I maintain. The advantages of the Hashtable are needless to say.    "Disadvantage" Hashtable is a very important problem is rehashing, if the data volume is large enough, Hashtable space will always run out, then need rehashing, rehashing is a very waste of time, reference to the expansion of the vector. For example, in PHP, large Web sites use memcached, which is a large in-memory hashtable, which stores a wide variety of data, so in the rehashing will waste a lot of time and in this time to deny access. This will increase the pressure on the database, and even overwhelm the site down. So, either set the memcached to the upper limit of available memory at the outset, or overwrite or erase data from the memcached without continuing to provide it. Or.....        Don't add as much data to it. "Other" 1, in addition to linear detection and two detection in conflict prevention, can also use double hash and multi-hash, the purpose is to try to keep the inserted data more evenly in the memory space of Hashtable, to prevent the conflict. 2, to prevent conflict, we must use the Hashfunc, good hashfunc will try to achieve the results mentioned in the 1th above. Here is an Apache bottom Hashtable hashfunc, as follows:
//这是一个Apache底层的一个hashfunc,Magic Number 33,33只是试验得出的而已
//这里以一个string为例,将字符串中的每一个字母转换成33进制,然后对hashtable取模运算
int Hashfunc (String key) {int sum=0for (int i=0;i<  Key.length (); + +i) {sum=sum* +(int) (Key.charat (i)); Sum=sum% hash_table_size;} return sum;}

Hash_set:    Hash_set and set are used in the same way, except that the implementation mechanism is different at the bottom.    The "Implementation" set is mostly implemented in Rb-tree, and Hash_set only invokes the Hashtable operation instead. Set is used to quickly find elements, both Rb-tree and Hashtable meet this requirement, except that Rb-tree does not allow duplication of data, and set can be automatically sorted by key values.    The "advantage" is the lookup.    The "disadvantage" hash_set is more efficient than set, but it does not have other properties based on the Rb-tree set.    The "other" hash_set and Hashtable constructs are actually the same, but their initial allocations are not the same size.  Hash_map:    Hash_map and map, the bottom layer is implemented using Hashtable, so hash_map does not have the ability to sort automatically.    In addition, other methods of use are the same, you can refer to my "STL book notes" in the face of map description.

Vectors, Hashtable, HashSet and Hashtable

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.