다음을 통해 공유


hash_map Class

Note

This API is obsolete. The alternative is unordered_map Class.

Stores and retrieves data quickly from a collection in which each element is a pair that has a sort key whose value is unique and an associated data value.

template <
   class Key, 
   class Type, 
   class Traits=hash_compare<Key, less<Key> >, 
   class Allocator=allocator<pair <const Key, Type> > 
>
class hash_map

Parameters

  • Key
    The key data type to be stored in the hash_map.

  • Type
    The element data type to be stored in the hash_map.

  • Traits
    The type which includes two function objects, one of class compare able to compare two element values as sort keys to determine their relative order and a hash function that is a unary predicate mapping key values of the elements to unsigned integers of type size_t. This argument is optional, and hash_compare<Key, less<Key> > is the default value.

  • Allocator
    The type that represents the stored allocator object that encapsulates details about the hash_map's allocation and deallocation of memory. This argument is optional, and the default value is allocator<pair <const Key, Type>>.

Remarks

The hash_map is:

  • An associative container, which a variable size container that supports the efficient retrieval of element values based on an associated key value.

  • Reversible, because it provides a bidirectional iterator to access its elements.

  • Hashed, because its elements are grouped into buckets based on the value of a hash function applied to the key values of the elements.

  • Unique in the sense that each of its elements must have a unique key.

  • A pair associative container, because its element data values are distinct from its key values.

  • A template class, because the functionality it provides is generic and so independent of the specific type of data contained as elements or keys. The data types to be used for elements and keys are, instead, specified as parameters in the class template along with the comparison function and allocator.

The main advantage of hashing over sorting is greater efficiency; a successful hashing performs insertions, deletions, and finds in constant average time as compared with a time proportional to the logarithm of the number of elements in the container for sorting techniques. The value of an element in a hash_map, but not its associated key value, may be changed directly. Instead, key values associated with old elements must be deleted and new key values associated with new elements inserted.

The choice of container type should be based in general on the type of searching and inserting required by the application. Hashed associative containers are optimized for the operations of lookup, insertion and removal. The member functions that explicitly support these operations are efficient when used with a well-designed hash function, performing them in a time that is on average constant and not dependent on the number of elements in the container. A well-designed hash function produces a uniform distribution of hashed values and minimizes the number of collisions, where a collision is said to occur when distinct key values are mapped into the same hashed value. In the worst case, with the worst possible hash function, the number of operations is proportional to the number of elements in the sequence (linear time).

The hash_map should be the associative container of choice when the conditions associating the values with their keys are satisfied by the application. A model for this type of structure is an ordered list of uniquely occurring keywords with associated string values providing, say, definitions. If, instead, the words had more than one correct definition, so that keys were not unique, then a hash_multimap would be the container of choice. If, on the other hand, just the list of words were being stored, then a hash_set would be the correct container. If multiple occurrences of the words were allowed, then a hash_multiset would be the appropriate container structure.

The hash_map orders the sequence it controls by calling a stored hash Traits object of class value_compare. This stored object may be accessed by calling the member function key_comp. Such a function object must behave the same as an object of class hash_compare<Key, less<Key>>. Specifically, for all values Key of type Key, the call Traits(Key ) yields a distribution of values of type size_t.

In general, the elements need be merely less than comparable to establish this order: so that, given any two elements, it may be determined either that they are equivalent (in the sense that neither is less than the other) or that one is less than the other. This results in an ordering between the nonequivalent elements. On a more technical note, the comparison function is a binary predicate that induces a strict weak ordering in the standard mathematical sense. A binary predicate f(x y) is a function object that has two argument objects x and y and a return value of true or false. An ordering imposed on a hash_map is a strict weak ordering if the binary predicate is irreflexive, antisymmetric, and transitive and if equivalence is transitive, where two objects x and y are defined to be equivalent when both f(x, y) and f(y, x) are false. If the stronger condition of equality between keys replaces that of equivalence, then the ordering becomes total (in the sense that all the elements are ordered with respect to each other) and the keys matched will be indiscernible from each other.

The actual order of elements in the controlled sequence depends on the hash function, the ordering function, and the current size of the hash table stored in the container object. You cannot determine the current size of the hash table, so you cannot in general predict the order of elements in the controlled sequence. Inserting elements invalidates no iterators, and removing elements invalidates only those iterators that had specifically pointed at the removed elements.

The iterator provided by the hash_map class is a bidirectional iterator, but the class member functions insert and hash_map have versions that take as template parameters a weaker input iterator, whose functionality requirements are more minimal than those guaranteed by the class of bidirectional iterators. The different iterator concepts form a family related by refinements in their functionality. Each iterator concept has its own set of requirements, and the algorithms that work with them must limit their assumptions to the requirements provided by that type of iterator. It may be assumed that an input iterator may be dereferenced to refer to some object and that it may be incremented to the next iterator in the sequence. This is a minimal set of functionality, but it is enough to be able to talk meaningfully about a range of iterators [First, Last) in the context of the class member functions.

In Visual C++ .NET 2003, members of the <hash_map> and <hash_set> header files are no longer in the std namespace, but rather have been moved into the stdext namespace. See The stdext Namespace for more information.

Constructors

hash_map

Constructs a hash_map that is empty or that is a copy of all or part of some other hash_map.

Typedefs

allocator_type

A type that represents the allocator class for the hash_map object.

const_iterator

A type that provides a bidirectional iterator that can read a const element in the hash_map.

const_pointer

A type that provides a pointer to a const element in a hash_map.

const_reference

A type that provides a reference to a const element stored in a hash_map for reading and performing const operations.

const_reverse_iterator

A type that provides a bidirectional iterator that can read any const element in the hash_map.

difference_type

A signed integer type that can be used to represent the number of elements of a hash_map in a range between elements pointed to by iterators.

iterator

A type that provides a bidirectional iterator that can read or modify any element in a hash_map.

key_compare

A type that provides a function object that can compare two sort keys to determine the relative order of two elements in the hash_map.

key_type

A type describes the sort key object that constitutes each element of the hash_map.

mapped_type

A type that represents the data type stored in a hash_map.

pointer

A type that provides a pointer to an element in a hash_map.

reference

A type that provides a reference to an element stored in a hash_map.

reverse_iterator

A type that provides a bidirectional iterator that can read or modify an element in a reversed hash_map.

size_type

An unsigned integer type that can represent the number of elements in a hash_map.

value_type

A type that provides a function object that can compare two elements as sort keys to determine their relative order in the hash_map.

Member Functions

hash_map::at

Finds an element in a hash_map with a specified key value.

begin

Returns an iterator addressing the first element in the hash_map.

hash_map::cbegin

Returns a const iterator addressing the first element in the hash_map.

hash_map::cend

Returns a const iterator that addresses the location succeeding the last element in a hash_map.

clear

Erases all the elements of a hash_map.

count

Returns the number of elements in a hash_map whose key matches a parameter-specified key.

hash_map::crbegin

Returns a const iterator addressing the first element in a reversed hash_map.

hash_map::crend

Returns a const iterator that addresses the location succeeding the last element in a reversed hash_map.

hash_map::emplace

Inserts an element constructed in place into a hash_map.

hash_map::emplace_hint

Inserts an element constructed in place into a hash_map, with a placement hint.

empty

Tests if a hash_map is empty.

end

Returns an iterator that addresses the location succeeding the last element in a hash_map.

equal_range

Returns a pair of iterators, respectively, to the first element in a hash_map with a key that is greater than a specified key and to the first element in the hash_map with a key that is equal to or greater than the key.

erase

Removes an element or a range of elements in a hash_map from specified positions

find

Returns an iterator addressing the location of an element in a hash_map that has a key equivalent to a specified key.

get_allocator

Returns a copy of the allocator object used to construct the hash_map.

insert

Inserts an element or a range of elements into a hash_map.

key_comp

Returns an iterator to the first element in a hash_map with a key value that is equal to or greater than that of a specified key.

lower_bound

Returns an iterator to the first element in a hash_map with a key value that is equal to or greater than that of a specified key.

max_size

Returns the maximum length of the hash_map.

rbegin

Returns an iterator addressing the first element in a reversed hash_map.

rend

Returns an iterator that addresses the location succeeding the last element in a reversed hash_map.

size

Returns the number of elements in the hash_map.

swap

Exchanges the elements of two hash_maps.

upper_bound

Returns an iterator to the first element in a hash_map that with a key value that is greater than that of a specified key.

value_comp

Retrieves a copy of the comparison object used to order element values in a hash_map.

Operators

operator[]

Inserts an element into a hash_map with a specified key value.

hash_map::operator=

Replaces the elements of a hash_map with a copy of another hash_map.

Requirements

Header: <hash_map>

Namespace: stdext

See Also

Reference

Thread Safety in the C++ Standard Library

Standard Template Library