Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Click here to view the latest version of this page.
PrevUpHomeNext

Class template unordered_multimap

boost::unordered_multimap — An unordered associative container that associates keys with another value. The same key can be stored multiple times.

Synopsis

// In header: <boost/unordered_map.hpp>

template<typename Key, typename Mapped, typename Hash = boost::hash<Key>, 
         typename Pred = std::equal_to<Key>, 
         typename Alloc = std::allocator<std::pair<Key const, Mapped> > > 
class unordered_multimap {
public:
  // types
  typedef Key                                      key_type;            
  typedef std::pair<Key const, Mapped>             value_type;          
  typedef Mapped                                   mapped_type;         
  typedef Hash                                     hasher;              
  typedef Pred                                     key_equal;           
  typedef Alloc                                    allocator_type;      
  typedef typename allocator_type::pointer         pointer;             
  typedef typename allocator_type::const_pointer   const_pointer;       
  typedef typename allocator_type::reference       reference;             // lvalue of value_type.
  typedef typename allocator_type::const_reference const_reference;       // const lvalue of value_type.
  typedef implementation-defined size_type;
  typedef implementation-defined difference_type;
  typedef implementation-defined iterator;
  typedef implementation-defined const_iterator;
  typedef implementation-defined local_iterator;
  typedef implementation-defined const_local_iterator;

  // construct/copy/destruct
  explicit unordered_multimap(size_type = implementation-defined, 
                              hasher const& = hasher(), 
                              key_equal const& = key_equal(), 
                              allocator_type const& = allocator_type());
  template<typename InputIterator> 
    unordered_multimap(InputIterator, InputIterator, 
                       size_type = implementation-defined, 
                       hasher const& = hasher(), 
                       key_equal const& = key_equal(), 
                       allocator_type const& = allocator_type());
  unordered_multimap(unordered_multimap const&);
  unordered_multimap(unordered_multimap &&);
  explicit unordered_multimap(Allocator const&);
  unordered_multimap(unordered_multimap const&, Allocator const&);
  ~unordered_multimap();
  unordered_multimap& operator=(unordered_multimap const&);
  unordered_multimap& operator=(unordered_multimap &&);
  allocator_type get_allocator() const;

  // size and capacity
  bool empty() const;
  size_type size() const;
  size_type max_size() const;

  // iterators
  iterator begin();
  const_iterator begin() const;
  iterator end();
  const_iterator end() const;
  const_iterator cbegin() const;
  const_iterator cend() const;

  // modifiers
  template<typename... Args> iterator emplace(Args&&...);
  template<typename... Args> iterator emplace_hint(const_iterator, Args&&...);
  iterator insert(value_type const&);
  iterator insert(const_iterator, value_type const&);
  template<typename InputIterator> void insert(InputIterator, InputIterator);
  iterator erase(const_iterator);
  size_type erase(key_type const&);
  iterator erase(const_iterator, const_iterator);
  void clear();
  void swap(unordered_multimap&);

  // observers
  hasher hash_function() const;
  key_equal key_eq() const;

  // lookup
  iterator find(key_type const&);
  iterator find(key_type const&) const;
  size_type count(key_type const&) const;
  std::pair<iterator, iterator> equal_range(key_type const&);
  std::pair<const_iterator, const_iterator> equal_range(key_type const&) const;

  // bucket interface
  size_type bucket_count() const;
  size_type max_bucket_count() const;
  size_type bucket_size(size_type) const;
  size_type bucket(key_type const&) const;
  local_iterator begin(size_type);
  const_local_iterator begin(size_type) const;
  local_iterator end(size_type);
  const_local_iterator end(size_type) const;
  const_local_iterator cbegin(size_type) const;
  const_local_iterator cend(size_type);

  // hash policy
  float load_factor() const;
  float max_load_factor() const;
  void max_load_factor(float);
  void rehash(size_type);
};

// Equality Comparisons
template<typename Key, typename Mapped, typename Hash, typename Pred, 
         typename Alloc> 
  bool operator==(unordered_multimap<Key, Mapped, Hash, Pred, Alloc> const&, 
                  unordered_multimap<Key, Mapped, Hash, Pred, Alloc> const&);
template<typename Key, typename Mapped, typename Hash, typename Pred, 
         typename Alloc> 
  bool operator!=(unordered_multimap<Key, Mapped, Hash, Pred, Alloc> const&, 
                  unordered_multimap<Key, Mapped, Hash, Pred, Alloc> const&);

// swap
template<typename Key, typename Mapped, typename Hash, typename Pred, 
         typename Alloc> 
  void swap(unordered_multimap<Key, Mapped, Hash, Pred, Alloc>&, 
            unordered_multimap<Key, Mapped, Hash, Pred, Alloc>&);

Description

Based on chapter 23 of the working draft of the C++ standard [n2960]. But without the updated rules for allocators.

Template Parameters

Key Key must be Assignable and CopyConstructible.
Mapped Mapped must be CopyConstructible
Hash A unary function object type that acts a hash function for a Key. It takes a single argument of type Key and returns a value of type std::size_t.
Pred A binary function object that implements an equivalence relation on values of type Key. A binary function object that induces an equivalence relation on values of type Key. It takes two arguments of type Key and returns a value of type bool.
Alloc An allocator whose value type is the same as the container's value type.

The elements are organized into buckets. Keys with the same hash code are stored in the same bucket and elements with equivalent keys are stored next to each other.

The number of buckets can be automatically increased by a call to insert, or as the result of calling rehash.

unordered_multimap public types

  1. typedef implementation-defined size_type;

    An unsigned integral type.

    size_type can represent any non-negative value of difference_type.

  2. typedef implementation-defined difference_type;

    A signed integral type.

    Is identical to the difference type of iterator and const_iterator.

  3. typedef implementation-defined iterator;

    A iterator whose value type is value_type.

    The iterator category is at least a forward iterator.

    Convertible to const_iterator.

  4. typedef implementation-defined const_iterator;

    A constant iterator whose value type is value_type.

    The iterator category is at least a forward iterator.

  5. typedef implementation-defined local_iterator;

    An iterator with the same value type, difference type and pointer and reference type as iterator.

    A local_iterator object can be used to iterate through a single bucket.

  6. typedef implementation-defined const_local_iterator;

    A constant iterator with the same value type, difference type and pointer and reference type as const_iterator.

    A const_local_iterator object can be used to iterate through a single bucket.

unordered_multimap public construct/copy/destruct

  1. explicit unordered_multimap(size_type n = implementation-defined, 
                                hasher const& hf = hasher(), 
                                key_equal const& eq = key_equal(), 
                                allocator_type const& a = allocator_type());

    Constructs an empty container with at least n buckets, using hf as the hash function, eq as the key equality predicate, a as the allocator and a maximum load factor of 1.0.

    Postconditions:

    size() == 0
  2. template<typename InputIterator> 
      unordered_multimap(InputIterator f, InputIterator l, 
                         size_type n = implementation-defined, 
                         hasher const& hf = hasher(), 
                         key_equal const& eq = key_equal(), 
                         allocator_type const& a = allocator_type());

    Constructs an empty container with at least n buckets, using hf as the hash function, eq as the key equality predicate, a as the allocator and a maximum load factor of 1.0 and inserts the elements from [f, l) into it.

  3. unordered_multimap(unordered_multimap const&);

    The copy constructor. Copies the contained elements, hash function, predicate, maximum load factor and allocator.

    Requires:

    value_type is copy constructible

  4. unordered_multimap(unordered_multimap &&);

    The move constructor.

    Notes:

    This is emulated on compilers without rvalue references.

    Requires:

    value_type is move constructible. (TODO: This is not actually required in this implementation).

  5. explicit unordered_multimap(Allocator const& a);

    Constructs an empty container, using allocator a.

  6. unordered_multimap(unordered_multimap const& x, Allocator const& a);

    Constructs an container, copying x's contained elements, hash function, predicate, maximum load factor, but using allocator a.

  7. ~unordered_multimap();

    Notes:

    The destructor is applied to every element, and all memory is deallocated

unordered_multimap& operator=(unordered_multimap const&);

The assignment operator. Copies the contained elements, hash function, predicate and maximum load factor but not the allocator.

Notes:

On compilers without rvalue references, there is a single assignment operator with the signature operator=(unordered_multimap) in order to emulate move semantics.

Requires:

value_type is copy constructible

unordered_multimap& operator=(unordered_multimap &&);

The move assignment operator.

Notes:

On compilers without rvalue references, there is a single assignment operator with the signature operator=(unordered_multimap) in order to emulate move semantics.

Requires:

value_type is move constructible. (TODO: This is not actually required in this implementation).

allocator_type get_allocator() const;

unordered_multimap size and capacity

  1. bool empty() const;

    Returns:

    size() == 0
  2. size_type size() const;

    Returns:

    std::distance(begin(), end())
  3. size_type max_size() const;

    Returns:

    size() of the largest possible container.

unordered_multimap iterators

  1. iterator begin();
    const_iterator begin() const;

    Returns:

    An iterator referring to the first element of the container, or if the container is empty the past-the-end value for the container.
  2. iterator end();
    const_iterator end() const;

    Returns:

    An iterator which refers to the past-the-end value for the container.
  3. const_iterator cbegin() const;

    Returns:

    A constant iterator referring to the first element of the container, or if the container is empty the past-the-end value for the container.
  4. const_iterator cend() const;

    Returns:

    A constant iterator which refers to the past-the-end value for the container.

unordered_multimap modifiers

  1. template<typename... Args> iterator emplace(Args&&... args);

    Inserts an object, constructed with the arguments args, in the container.

    Returns:

    An iterator pointing to the inserted element.

    Throws:

    If an exception is thrown by an operation other than a call to hasher the function has no effect.

    Notes:

    Can invalidate iterators, but only if the insert causes the load factor to be greater to or equal to the maximum load factor.

    Pointers and references to elements are never invalidated.

    If the compiler doesn't support variadic template arguments or rvalue references, this is emulated for up to 10 arguments, with no support for rvalue references or move semantics.

  2. template<typename... Args> 
      iterator emplace_hint(const_iterator hint, Args&&... args);

    Inserts an object, constructed with the arguments args, in the container.

    hint is a suggestion to where the element should be inserted.

    Returns:

    An iterator pointing to the inserted element.

    Throws:

    If an exception is thrown by an operation other than a call to hasher the function has no effect.

    Notes:

    The standard is fairly vague on the meaning of the hint. But the only practical way to use it, and the only way that Boost.Unordered supports is to point to an existing element with the same key.

    Can invalidate iterators, but only if the insert causes the load factor to be greater to or equal to the maximum load factor.

    Pointers and references to elements are never invalidated.

    If the compiler doesn't support variadic template arguments or rvalue references, this is emulated for up to 10 arguments, with no support for rvalue references or move semantics.

  3. iterator insert(value_type const& obj);

    Inserts obj in the container.

    Returns:

    An iterator pointing to the inserted element.

    Throws:

    If an exception is thrown by an operation other than a call to hasher the function has no effect.

    Notes:

    Can invalidate iterators, but only if the insert causes the load factor to be greater to or equal to the maximum load factor.

    Pointers and references to elements are never invalidated.

  4. iterator insert(const_iterator hint, value_type const& obj);

    Inserts obj in the container.

    hint is a suggestion to where the element should be inserted.

    Returns:

    An iterator pointing to the inserted element.

    Throws:

    If an exception is thrown by an operation other than a call to hasher the function has no effect.

    Notes:

    The standard is fairly vague on the meaning of the hint. But the only practical way to use it, and the only way that Boost.Unordered supports is to point to an existing element with the same key.

    Can invalidate iterators, but only if the insert causes the load factor to be greater to or equal to the maximum load factor.

    Pointers and references to elements are never invalidated.

  5. template<typename InputIterator> 
      void insert(InputIterator first, InputIterator last);

    Inserts a range of elements into the container.

    Throws:

    When inserting a single element, if an exception is thrown by an operation other than a call to hasher the function has no effect.

    Notes:

    Can invalidate iterators, but only if the insert causes the load factor to be greater to or equal to the maximum load factor.

    Pointers and references to elements are never invalidated.

  6. iterator erase(const_iterator position);

    Erase the element pointed to by position.

    Returns:

    The iterator following position before the erasure.

    Throws:

    Only throws an exception if it is thrown by hasher or key_equal.

    In this implementation, this overload doesn't call either function object's methods so it is no throw, but this might not be true in other implementations.

  7. size_type erase(key_type const& k);

    Erase all elements with key equivalent to k.

    Returns:

    The number of elements erased.

    Throws:

    Only throws an exception if it is thrown by hasher or key_equal.

  8. iterator erase(const_iterator first, const_iterator last);

    Erases the elements in the range from first to last.

    Returns:

    The iterator following the erased elements - i.e. last.

    Throws:

    Only throws an exception if it is thrown by hasher or key_equal.

    In this implementation, this overload doesn't call either function object's methods so it is no throw, but this might not be true in other implementations.

  9. void clear();

    Erases all elements in the container.

    Postconditions:

    size() == 0

    Throws:

    Never throws an exception.

  10. void swap(unordered_multimap&);

    Throws:

    If the allocators are equal, doesn't throw an exception unless it is thrown by the copy constructor or copy assignment operator of key_equal or hasher.

    Notes:

    For a discussion of the behavior when allocators aren't equal see the implementation details.

unordered_multimap observers

  1. hasher hash_function() const;

    Returns:

    The container's hash function.
  2. key_equal key_eq() const;

    Returns:

    The container's key equality predicate.

unordered_multimap lookup

  1. iterator find(key_type const& k);
    iterator find(key_type const& k) const;

    Returns:

    An iterator pointing to an element with key equivalent to k, or b.end() if no such element exists.

  2. size_type count(key_type const& k) const;

    Returns:

    The number of elements with key equivalent to k.

  3. std::pair<iterator, iterator> equal_range(key_type const& k);
    std::pair<const_iterator, const_iterator> equal_range(key_type const& k) const;

    Returns:

    A range containing all elements with key equivalent to k. If the container doesn't container any such elements, returns std::make_pair(b.end(),b.end()).

unordered_multimap bucket interface

  1. size_type bucket_count() const;

    Returns:

    The number of buckets.

  2. size_type max_bucket_count() const;

    Returns:

    An upper bound on the number of buckets.

  3. size_type bucket_size(size_type n) const;

    Requires:

    n < bucket_count()

    Returns:

    The number of elements in bucket n.

  4. size_type bucket(key_type const& k) const;

    Returns:

    The index of the bucket which would contain an element with key k.

    Postconditions:

    The return value is less than bucket_count()

  5. local_iterator begin(size_type n);
    const_local_iterator begin(size_type n) const;

    Requires:

    n shall be in the range [0, bucket_count()).

    Returns:

    A local iterator pointing the first element in the bucket with index n.

  6. local_iterator end(size_type n);
    const_local_iterator end(size_type n) const;

    Requires:

    n shall be in the range [0, bucket_count()).

    Returns:

    A local iterator pointing the 'one past the end' element in the bucket with index n.

  7. const_local_iterator cbegin(size_type n) const;

    Requires:

    n shall be in the range [0, bucket_count()).

    Returns:

    A constant local iterator pointing the first element in the bucket with index n.

  8. const_local_iterator cend(size_type n);

    Requires:

    n shall be in the range [0, bucket_count()).

    Returns:

    A constant local iterator pointing the 'one past the end' element in the bucket with index n.

unordered_multimap hash policy

  1. float load_factor() const;

    Returns:

    The average number of elements per bucket.

  2. float max_load_factor() const;

    Returns:

    Returns the current maximum load factor.

  3. void max_load_factor(float z);

    Effects:

    Changes the container's maximum load factor, using z as a hint.

  4. void rehash(size_type n);

    Changes the number of buckets so that there at least n buckets, and so that the load factor is less than the maximum load factor.

    Invalidates iterators, and changes the order of elements. Pointers and references to elements are not invalidated.

    Throws:

    The function has no effect if an exception is thrown, unless it is thrown by the container's hash function or comparison function.

unordered_multimap Equality Comparisons

  1. template<typename Key, typename Mapped, typename Hash, typename Pred, 
             typename Alloc> 
      bool operator==(unordered_multimap<Key, Mapped, Hash, Pred, Alloc> const& x, 
                      unordered_multimap<Key, Mapped, Hash, Pred, Alloc> const& y);

    Notes:

    This is a boost extension.

    Behavior is undefined if the two containers don't have equivalent equality predicates.

  2. template<typename Key, typename Mapped, typename Hash, typename Pred, 
             typename Alloc> 
      bool operator!=(unordered_multimap<Key, Mapped, Hash, Pred, Alloc> const& x, 
                      unordered_multimap<Key, Mapped, Hash, Pred, Alloc> const& y);

    Notes:

    This is a boost extension.

    Behavior is undefined if the two containers don't have equivalent equality predicates.

unordered_multimap swap

  1. template<typename Key, typename Mapped, typename Hash, typename Pred, 
             typename Alloc> 
      void swap(unordered_multimap<Key, Mapped, Hash, Pred, Alloc>& x, 
                unordered_multimap<Key, Mapped, Hash, Pred, Alloc>& y);

    Effects:

    x.swap(y)

    Throws:

    If the allocators are equal, doesn't throw an exception unless it is thrown by the copy constructor or copy assignment operator of Hash or Pred.

    Notes:

    For a discussion of the behavior when allocators aren't equal see the implementation details.


PrevUpHomeNext