Unordered associative container (c++11)

Source: Internet
Author: User
Tags rehash

2012-11-27 15:22 Bria/Ching Translation of the People's post and telecommunications publishing house font size: t| T

C++primer Plus (6th edition) (Chinese version) appendix G Standard Template Library methods and functions: This appendix summarizes STL container methods and common STL algorithm functions. This section introduces the unordered associative containers (c++11).

AD:

G.4 unordered associative container (c++11)

As mentioned earlier, the unordered associative containers (unordered_set, Unordered_multiset, Unordered_map, and unordered_multimap) use keys and hash tables so that data can be accessed quickly. These concepts are briefly described in the following. The hash function converts the key to an indexed value. For example, if the key is a string object, the hash function may add the numeric encoding of each character in it, and then calculate the remainder of the result divided by 13, resulting in an index of 0~12. The unordered container will use 13 buckets (buckets) to store the string, and all strings indexed as 4 will be stored in the 4th bucket. If you are searching for a key in a container, a hash function is executed against the key, which in turn is searched only in the corresponding bucket of the index. Ideally, there should be enough buckets, with only a few strings in each bucket.

The C++11 library provides template hash<key>, and the unordered associative container uses the template by default. The template's materialization is defined for various integral types, floating-point types, pointers, and some template classes, such as String.

Table g.11 lists the types that are used for these containers.

An interface for an unordered associative container is similar to an associative container. Specifically, table g.10 also applies to unordered associative containers, with the following exceptions: No method Lower_bound () and Upper_bound () are required, and constructor X (i, J, c) is also true. Regular associative containers are ordered, which allows them to use comparison predicates that represent the concept of "less than". This comparison does not apply to unordered associative containers, so they use a comparison predicate based on the concept "equals".

Table g.11 types defined for unordered associative containers

Type

Value

X::key_type

Key, type

X::key_equal

Pred, a two-dollar predicate that examines two types

Whether the parameters for key are equal

Continuation table

hash, one such two-dollar function object, that is, if the

/table>

x::hasher

x::local_iterator

one type Same iterator as X::iterator, but only for one bucket

x::const_local_iterator

x::mapped_type

t, associated data type (map and Multimap only)

In addition to the methods shown in table g.10, the unordered associative container contains some other essential methods, as shown in table g.12. In this table, X is an unordered associative container class, A is an object of type X, B may be a constant object of type X, A_uniq is an object of type Unordered_set or Unordered_map, a_eq is of type unordered_ Multiset or Unordered_multimap object, HF is a value of type Hasher, EQ is a value of type key_equal, n is a value of type Size_type, Z is a value of type float. As before, I and J are also input iterators to the value_type element, [I, j] is an effective interval, p and Q2 are iterators that point to a, Q and Q1 are dereference iterators to a, [Q1, Q2] are valid intervals, T is X::value_ The type value (possibly a pair), K is the X::key_type value, and IL is the Initializer_list<value_type> object.

Table g.12 actions defined for unordered associative containers

Operation

Description

X (n, HF, EQ)

Create an empty container with at least n buckets and use the HF

The Thohashi function uses the EQ as the key-value equality predicate. If the province

If the EQ is omitted, the key_equal () is used as the key-value equality predicate;

HF is also omitted, the Hasher () is used as a hash function

X A (n, HF, EQ)

Create an empty container named A, which contains at least n buckets,

The HF is used as a hash function, and the EQ is used as the key-value equality predicate.

If the EQ is omitted, the key_equal () is used as the key-value equality predicate;

If HF is also omitted, the Hasher () is used as the hash function

X (i, J, N, HF, EQ)

Create an empty container with at least n buckets, using HF as a hash

function, use EQ as the key-value equality predicate, and insert the interval [I, j]

The element in the. If the EQ is omitted, key_equal () is used as the key value

Equality predicate; If HF is omitted, Hasher () is used as a hash

function, if n is omitted, contains an indeterminate number of buckets

X A (I, J, N, HF, EQ)

Create an empty container named A, which contains at least n buckets,

Use HF as a hash function, use EQ as the key-value equality predicate, and

Inserts an element in the interval [I, j]. If the EQ is omitted, the

Key_equal () is used as a key-value equality predicate, if HF is omitted,

Use Hasher () as a hash function, or if n is omitted, contains an indeterminate number of buckets

B.hash_function ()

Returns the hash function used by b

B.key_eq ()

Returns the key-value equality predicate used when creating B

B.bucket_count ()

Returns the number of buckets contained in B

B.max_bucket_count ()

Returns a limit number that specifies how many buckets a B can contain

B.bucket (k)

Returns the index of the bucket to which the element with the key value K belongs

B.bucket_size (N)

Returns the number of elements that a bucket indexed to n can contain

B.begin (N)

Returns an iterator that points to the first element in a bucket indexed to n

B.end (N)

Returns an iterator that points to the last element in a bucket indexed to n

B.cbegin (N)

Returns a const iterator that points to the first element in a bucket indexed to n

B.cend (N)

Returns a const iterator that points to the last element in a bucket indexed to n

B.load_factor ()

Returns the average number of elements that each bucket contains

B.max_load_factor ()

Returns the maximum possible value of the load factor;

The container will increase the bucket

B.max_load_factor (z)

May modify the maximum load system, it is recommended to set it to Z

A.rehash (N)

Adjust the number of buckets to not less than N and ensure that the A.bucket_count ()

> a.size ()/A.max_load_factor ()

A.reserve (N)

Equivalent to A.rehash (Ceil (N/a.max_load_factor ())),

where Ceil (x) returns the smallest integer not less than X

Related Article

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.