全部博文(37)
分类: C/C++
2008-06-21 16:50:05
Description
Hash_map 是一种使用hash 关联容器,把Key 和value数据对应存储; Hash_map 同样是一个Pair 的关联容器,这意味着其元素类型是pair 由于hash_map在通过key值查找时具有很高的效率,所以hash_map对于一些互不相干的元素的存储非常有用。如果元素的某种顺序比较重要,使用map更合适一些。
Example
Definition Defined in the header hash_map, and in the backward-compatibility header hash_map.h. This class is an SGI extension; it is not part of the C++ standard.
const_reference
as the hash function.
These members are not defined in the and requirements, but are specific to hash_map.
Hash_map::iterator is not a mutable iterator, because hash_map::value_type is not . That is, if i is of type hash_map::iterator and p is of type
hash_map::value_type, then *i = p is not a valid expression. However, hash_map::iterator isn't a constant iterator either, because it can be used to modify the object that it points to. Using the same notation as above, (*i).second = p is a valid expression.
This member function relies on member template functions, which at present (early 1998) are not supported by all compilers. If your compiler supports member templates, you can call this function with any type of . If your compiler does not yet support member templates, though, then the arguments must either be of type const value_type* or of type
hash_map::const_iterator.
Since operator[] might insert a new element into the hash_map, it can't possibly be a const member function. Note that the definition of operator[] is extremely simple: m[k] is equivalent to
(*((m.insert(value_type(k, data_type()))).first)).second. Strictly speaking, this member function is unnecessary: it exists only for convenience.
, , ,
-- Main.WinterWen - 15 Aug 2005 struct eqstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) == 0;
}
};
int main()
{
hash_map<const char*, int, hash<const char*>, eqstr> months;
months["january"] = 31;
months["february"] = 28;
months["march"] = 31;
months["april"] = 30;
months["may"] = 31;
months["june"] = 30;
months["july"] = 31;
months["august"] = 31;
months["september"] = 30;
months["october"] = 31;
months["november"] = 30;
months["december"] = 31;
cout << "september -> " << months["september"] << endl;
cout << "april -> " << months["april"] << endl;
cout << "june -> " << months["june"] << endl;
cout << "november -> " << months["november"] << endl;
}
Template parameters
Parameter
Description
Default
Key
The hash_map's key type. This is also defined as hash_map::key_type.
Data
The hash_map's data type. This is also defined as hash_map::data_type.
HashFcn
The used by the hash_map. This is also defined as hash_map::hasher.
EqualKey
The hash_map key equality function: a that determines whether two keys are equal. This is also defined as hash_map::key_equal.
Alloc
The hash_map's allocator, used for all internal memory management.
Members
Member
Where defined
Description
key_type
The hash_map's key type, Key.
data_type
The type of object associated with the keys.
value_type
The type of object, pair
hasher
The hash_map's .
key_equal
that compares keys for equality.
pointer
Pointer to T.
reference
Reference to T
Const reference to T
size_type
An unsigned integral type.
difference_type
A signed integral type.
iterator
Iterator used to iterate through a hash_map.
const_iterator
Const iterator used to iterate through a hash_map.
iterator begin()
Returns an iterator pointing to the beginning of the hash_map.
iterator end()
Returns an iterator pointing to the end of the hash_map.
const_iterator begin() const
Returns an const_iterator pointing to the beginning of the hash_map.
const_iterator end() const
Returns an const_iterator pointing to the end of the hash_map.
size_type size() const
Returns the size of the hash_map.
size_type max_size() const
Returns the largest possible size of the hash_map.
bool empty() const
true if the hash_map's size is 0.
size_type bucket_count() const
Returns the number of buckets used by the hash_map.
void resize(size_type n)
Increases the bucket count to at least n.
hasher hash_funct() const
Returns the hasher object used by the hash_map.
key_equal key_eq() const
Returns the key_equal object used by the hash_map.
hash_map()
Creates an empty hash_map.
hash_map(size_type n)
Creates an empty hash_map with at least n buckets.
hash_map(size_type n,
const hasher& h)
Creates an empty hash_map with at least n buckets, using h
hash_map(size_type n,
const hasher& h,
const key_equal& k)
Creates an empty hash_map with at least n buckets, using h as the hash function and k as the key equal function.
template
Creates a hash_map with a copy of a range.
template
Creates a hash_map with a copy of a range and a bucket count of at least n.
template
Creates a hash_map with a copy of a range and a bucket count of at least n, using h as the hash function.
template
Creates a hash_map with a copy of a range and a bucket count of at least n, using h as the hash function and k as the key equal function.
hash_map(const hash_map&)
The copy constructor.
hash_map& operator=(const hash_map&)
The assignment operator
void swap(hash_map&)
Swaps the contents of two hash_maps.
pair
Inserts x into the hash_map.
template
Inserts a range into the hash_map.
void erase(iterator pos)
Erases the element pointed to by pos.
size_type erase(const key_type& k)
Erases the element whose key is k.
void erase(iterator first, iterator last)
Erases all elements in a range.
void clear()
Erases all of the elements.
const_iterator find(const key_type& k) const
Finds an element whose key is k.
iterator find(const key_type& k)
Finds an element whose key is k.
size_type count(const key_type& k) const
Counts the number of elements whose key is k.
pair
Finds a range containing all elements whose key is k.
pair
Finds a range containing all elements whose key is k.
data_type&
operator[](const key_type& k)
hash_map
See below.
bool operator==(const hash_map&,
const hash_map&)
Tests two hash_maps for equality. This is a global function, not a member function. New members
Member
Description
data_type&
operator[](const key_type& k)
Returns a reference to the object that is associated with a particular key. If the hash_map does not already contain such an object, operator[] inserts the default object data_type().
Notes
See also
, , , , , ,