C + + header file series (Unordered_map, Unordered_set)

Source: Internet
Author: User

Brief introduction

Obviously, these two header files are the unordered version corresponding to the map and set header files respectively. So they have an important property:

    • Disorderly order

How to disorderly order

This unorder implies that the underlying implementation of the class in the two header files----Hash. And that's why you can pass in a custom hash function when declaring these unordered template classes, which is exactly the hash function object.

An element with the same hash value is placed in the same bucket (bucket).

Why the Order of Chaos

focus on fast acquisition of elements in the context of providing mappings and collection functionality .

With the tree structure (red black tree, binary search tree, etc.) implementation of the map, set, in the search, acquisition, modification of elements, usually need to go from the root node to traverse the tree structure from top to bottom, so the time complexity is linear , and through the hash table, as long as the hash function and the size of the bucket is selected properly, The time complexity will be constant (only one function is called and a small range of lookups is required).

One-way iterators

The implementation of the hash table complicates the bidirectional traversal on the container, and there seems to be no suitable method to be efficient and fast. Therefore, the Unorder version of map and set provides only forward iterators (non-unorder versions provide bidirectional iterators).

What functions are missing?
    • Lower_bound

    • Upper_bound

The normal version of the map and set, they are ordered containers, for each element can be judged by which it should be before, after which, and the version of the container is not the same, because they are disorderly , can not determine the order of each element . Therefore, the container does not have enough information to calculate the two boundaries (however, the equality comparison of the elements is still feasible).

What's the function?

For the sake of implementation, this version of the class template is essential to a number of special functions.

Bucket-related (bucket)
    • Bucket_count: Number of barrels.

    • Max_bucket_count: Maximum number of barrels.

    • Bucket_size: Bucket size, or capacity.

    • Bucket: Locates the bucket position of the given element.

Hash policy
    • Load_factor: Returns the load Factor, which is the ratio of the current number of elements in the container to the number of buckets .

    • Max_load_factor: Returns or sets the maximum load factor.

    • Rehash: Sets the number of buckets and re-hashes the elements.

    • Reserve: Requests the number of reserved buckets to a given value.

Notice that no function can change the capacity of the bucket, and The bucket may also grow dynamically .

Observers
    • Hash_function: Returns the hash function ( passed in as a parameter when declared, or the default hash in the Funtional header file).

    • KEY_EQ: Returns the equality predicate for key, similar to hash_function.


C + + header file series (Unordered_map, Unordered_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.