intro.qbk 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. [/ Copyright 2006-2008 Daniel James.
  2. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) ]
  4. [def __hash-table__ [@http://en.wikipedia.org/wiki/Hash_table
  5. hash table]]
  6. [def __hash-function__ [@http://en.wikipedia.org/wiki/Hash_function
  7. hash function]]
  8. [section:intro Introduction]
  9. For accessing data based on key lookup, the C++ standard library offers `std::set`,
  10. `std::map`, `std::multiset` and `std::multimap`. These are generally
  11. implemented using balanced binary trees so that lookup time has
  12. logarithmic complexity. That is generally okay, but in many cases a
  13. __hash-table__ can perform better, as accessing data has constant complexity,
  14. on average. The worst case complexity is linear, but that occurs rarely and
  15. with some care, can be avoided.
  16. Also, the existing containers require a 'less than' comparison object
  17. to order their elements. For some data types this is impossible to implement
  18. or isn't practical. In contrast, a hash table only needs an equality function
  19. and a hash function for the key.
  20. With this in mind, unordered associative containers were added to the C++
  21. standard. This is an implementation of the containers described in C++11,
  22. with some [link unordered.compliance deviations from the standard] in
  23. order to work with non-C++11 compilers and libraries.
  24. `unordered_set` and `unordered_multiset` are defined in the header
  25. <[headerref boost/unordered_set.hpp]>
  26. namespace boost {
  27. template <
  28. class Key,
  29. class Hash = ``[classref boost::hash]``<Key>,
  30. class Pred = std::equal_to<Key>,
  31. class Alloc = std::allocator<Key> >
  32. class ``[classref boost::unordered_set unordered_set]``;
  33. template<
  34. class Key,
  35. class Hash = ``[classref boost::hash]``<Key>,
  36. class Pred = std::equal_to<Key>,
  37. class Alloc = std::allocator<Key> >
  38. class ``[classref boost::unordered_multiset unordered_multiset]``;
  39. }
  40. `unordered_map` and `unordered_multimap` are defined in the header
  41. <[headerref boost/unordered_map.hpp]>
  42. namespace boost {
  43. template <
  44. class Key, class Mapped,
  45. class Hash = ``[classref boost::hash]``<Key>,
  46. class Pred = std::equal_to<Key>,
  47. class Alloc = std::allocator<std::pair<Key const, Mapped> > >
  48. class ``[classref boost::unordered_map unordered_map]``;
  49. template<
  50. class Key, class Mapped,
  51. class Hash = ``[classref boost::hash]``<Key>,
  52. class Pred = std::equal_to<Key>,
  53. class Alloc = std::allocator<std::pair<Key const, Mapped> > >
  54. class ``[classref boost::unordered_multimap unordered_multimap]``;
  55. }
  56. When using Boost.TR1, these classes are included from `<unordered_set>` and
  57. `<unordered_map>`, with the classes added to the `std::tr1` namespace.
  58. The containers are used in a similar manner to the normal associative
  59. containers:
  60. [import src_code/intro.cpp]
  61. [intro_example1_2]
  62. But since the elements aren't ordered, the output of:
  63. [intro_example1_3]
  64. can be in any order. For example, it might be:
  65. two,2
  66. one,1
  67. three,3
  68. To store an object in an unordered associative container requires both a
  69. key equality function and a hash function. The default function objects in
  70. the standard containers support a few basic types including integer types,
  71. floating point types, pointer types, and the standard strings. Since
  72. Boost.Unordered uses [classref boost::hash] it also supports some other types,
  73. including standard containers. To use any types not supported by these methods
  74. you have to [link hash.custom extend Boost.Hash to support the type] or use
  75. your own custom equality predicates and hash functions. See the
  76. [link unordered.hash_equality Equality Predicates and Hash Functions] section
  77. for more details.
  78. There are other differences, which are listed in the
  79. [link unordered.comparison Comparison with Associative Containers] section.
  80. [endsect]