Content deleted Content added
Undid revision 538798984 by Zombiezparadize (talk): The program instantiates "std::unordered_map<std::string, int>" and so needs to contain "#include <string>". |
Tom.Reding (talk | contribs) m →top: Confirm {{Use dmy dates}} from 2012 |
||
(14 intermediate revisions by 12 users not shown) | |||
Line 1:
{{Short description|Group of class templates in the C++ Standard
{{C++ Standard Library}}
In the programming language [[C++
The unordered associative containers are similar to the [[
==History==
The first widely used implementation of hash tables in the C++ language was <code>hash_map</code>, <code>hash_set</code>, <code>hash_multimap</code>, <code>hash_multiset</code> class templates of the [[Silicon Graphics
The <code>hash_*</code> class templates were proposed into [[C++ Technical Report 1
==Overview of functions==
The containers are defined in headers named after the names of the containers, e.g., <code>unordered_set</code> is defined in header <code><unordered_set></code>. All containers satisfy the requirements of the [http://www.sgi.com/tech/stl/Container.html Container] [[concept (generic programming)|concept]], which means they have <code>begin()</code>, <code>end()</code>, <code>size()</code>, <code>max_size()</code>, <code>empty()</code>, and <code>swap()</code> methods.
{| class="wikitable" style="font-size:0.85em"
|-
!
! <code>unordered_set</code><br />([[C++11]])
! <code>unordered_map</code><br />(
! <code>unordered_multiset</code><br />(
! <code>unordered_multimap</code><br />(
! Description
|-
! rowspan=4 |
| [http://en.cppreference.com/w/cpp/container/unordered_set/
| [http://en.cppreference.com/w/cpp/container/unordered_map/
| [http://en.cppreference.com/w/cpp/container/unordered_multiset/
| [http://en.cppreference.com/w/cpp/container/unordered_multimap/
| Constructs the container from variety of sources
|-
| [http://en.cppreference.com/w/cpp/container/unordered_set/~
| [http://en.cppreference.com/w/cpp/container/unordered_map/~
| [http://en.cppreference.com/w/cpp/container/unordered_multiset/~
| [http://en.cppreference.com/w/cpp/container/unordered_multimap/~
| Destructs the set and the contained elements
|-
Line 174 ⟶ 173:
==Usage example==
<
#include <iostream>
#include <string>
Line 200 ⟶ 199:
return 0;
}
</syntaxhighlight>
==Custom hash functions==
<
#include <unordered_map>
struct X{int i,j,k;};
struct hash_X{
size_t operator()(const X &x) const{
return std::hash<int>()(x.i) ^ std::hash<int>()(x.j) ^ std::hash<int>()(x.k);
}
};
</syntaxhighlight>
The user defined function can be used as is in std::unordered_map, by passing it as a template parameter
<
Or can be set as the default hash function by specializing the std::hash function
<
namespace std {
template <>
Line 231 ⟶ 232:
//...
std::unordered_map<X,int> my_map;
</syntaxhighlight>
==References==
{{
▲{{use dmy dates|date=January 2012}}
[[Category:Articles with example C++ code]]
|