ReferenceHeader <boost/unordered_set.hpp>namespaceboost{template<typename Value,typename Hash =boost::hash<Value>,typename Pred =std::equal_to<Value>,typename Alloc =std::allocator<Value>>class unordered_set;template<typename Value,typename Hash,typename Pred,typename Alloc>booloperator==(unordered_set<Value,Hash,Pred,Alloc>const&,unordered_set<Value,Hash,Pred,Alloc>const&);template<typename Value,typename Hash,typename Pred,typename Alloc>booloperator!=(unordered_set<Value,Hash,Pred,Alloc>const&,unordered_set<Value,Hash,Pred,Alloc>const&);template<typename Value,typename Hash,typename Pred,typename Alloc>voidswap(unordered_set<Value,Hash,Pred,Alloc>&,unordered_set<Value,Hash,Pred,Alloc>&);template<typename Value,typename Hash =boost::hash<Value>,typename Pred =std::equal_to<Value>,typename Alloc =std::allocator<Value>>class unordered_multiset;template<typename Value,typename Hash,typename Pred,typename Alloc>booloperator==(unordered_multiset<Value,Hash,Pred,Alloc>const&,unordered_multiset<Value,Hash,Pred,Alloc>const&);template<typename Value,typename Hash,typename Pred,typename Alloc>booloperator!=(unordered_multiset<Value,Hash,Pred,Alloc>const&,unordered_multiset<Value,Hash,Pred,Alloc>const&);template<typename Value,typename Hash,typename Pred,typename Alloc>voidswap(unordered_multiset<Value,Hash,Pred,Alloc>&,unordered_multiset<Value,Hash,Pred,Alloc>&);}Class template unordered_set3boost::unordered_set
An unordered associative container that stores unique values.
// In header: <boost/unordered_set.hpp>
template<typename Value,typename Hash =boost::hash<Value>,typename Pred =std::equal_to<Value>,typename Alloc =std::allocator<Value>>class unordered_set {public:// typestypedefValuekey_type;typedefValuevalue_type;typedefHashhasher;typedefPredkey_equal;typedefAllocallocator_type;typedeftypenameallocator_type::pointerpointer;typedeftypenameallocator_type::const_pointerconst_pointer;typedeftypenameallocator_type::referencereference;// lvalue of value_type.typedeftypenameallocator_type::const_referenceconst_reference;// const lvalue of value_type.typedefimplementation-definedsize_type;typedefimplementation-defineddifference_type;typedefimplementation-definediterator;typedefimplementation-definedconst_iterator;typedefimplementation-definedlocal_iterator;typedefimplementation-definedconst_local_iterator;// construct/copy/destructexplicitunordered_set(size_type=implementation-defined,hasherconst&=hasher(),key_equalconst&=key_equal(),allocator_typeconst&=allocator_type());template<typename InputIterator>unordered_set(InputIterator,InputIterator,size_type=implementation-defined,hasherconst&=hasher(),key_equalconst&=key_equal(),allocator_typeconst&=allocator_type());unordered_set(unordered_setconst&);unordered_set(unordered_set&&);explicitunordered_set(Allocatorconst&);unordered_set(unordered_setconst&,Allocatorconst&);~unordered_set();unordered_set&operator=(unordered_setconst&);unordered_set&operator=(unordered_set&&);allocator_typeget_allocator()const;// size and capacityboolempty()const;size_typesize()const;size_typemax_size()const;// iteratorsiteratorbegin();const_iteratorbegin()const;iteratorend();const_iteratorend()const;const_iteratorcbegin()const;const_iteratorcend()const;// modifierstemplate<typename... Args>std::pair<iterator,bool>emplace(Args&&...);template<typename... Args>iteratoremplace_hint(const_iterator,Args&&...);std::pair<iterator,bool>insert(value_typeconst&);iteratorinsert(const_iterator,value_typeconst&);template<typename InputIterator>voidinsert(InputIterator,InputIterator);iteratorerase(const_iterator);size_typeerase(key_typeconst&);iteratorerase(const_iterator,const_iterator);voidquick_erase(const_iterator);voiderase_return_void(const_iterator);voidclear();voidswap(unordered_set&);// observershasherhash_function()const;key_equalkey_eq()const;// lookupiteratorfind(key_typeconst&);const_iteratorfind(key_typeconst&)const;template<typename CompatibleKey,typename CompatibleHash,typename CompatiblePredicate>iteratorfind(CompatibleKeyconst&,CompatibleHashconst&,CompatiblePredicateconst&);template<typename CompatibleKey,typename CompatibleHash,typename CompatiblePredicate>const_iteratorfind(CompatibleKeyconst&,CompatibleHashconst&,CompatiblePredicateconst&)const;size_typecount(key_typeconst&)const;std::pair<iterator,iterator>equal_range(key_typeconst&);std::pair<const_iterator,const_iterator>equal_range(key_typeconst&)const;// bucket interfacesize_typebucket_count()const;size_typemax_bucket_count()const;size_typebucket_size(size_type)const;size_typebucket(key_typeconst&)const;local_iteratorbegin(size_type);const_local_iteratorbegin(size_type)const;local_iteratorend(size_type);const_local_iteratorend(size_type)const;const_local_iteratorcbegin(size_type)const;const_local_iteratorcend(size_type);// hash policyfloatload_factor()const;floatmax_load_factor()const;voidmax_load_factor(float);voidrehash(size_type);};// Equality Comparisonstemplate<typename Value,typename Hash,typename Pred,typename Alloc>booloperator==(unordered_set<Value,Hash,Pred,Alloc>const&,unordered_set<Value,Hash,Pred,Alloc>const&);template<typename Value,typename Hash,typename Pred,typename Alloc>booloperator!=(unordered_set<Value,Hash,Pred,Alloc>const&,unordered_set<Value,Hash,Pred,Alloc>const&);// swaptemplate<typename Value,typename Hash,typename Pred,typename Alloc>voidswap(unordered_set<Value,Hash,Pred,Alloc>&,unordered_set<Value,Hash,Pred,Alloc>&);DescriptionBased on chapter 23 of
the working draft of the C++ standard [n2960].
But without the updated rules for allocators.
Template ParametersValueValue must be Assignable and CopyConstructibleHashA unary function object type that acts a hash function for a Value. It takes a single argument of type Value and returns a value of type std::size_t.PredA binary function object that implements an equivalence relation on values of type Value.
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.AllocAn 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.The number of buckets can be automatically increased by a call to insert, or as the result of calling rehash.unordered_set
public
typestypedefimplementation-definedsize_type;An unsigned integral type.size_type can represent any non-negative value of difference_type.typedefimplementation-defineddifference_type;A signed integral type.Is identical to the difference type of iterator and const_iterator.typedefimplementation-definediterator;A constant iterator whose value type is value_type. The iterator category is at least a forward iterator.Convertible to const_iterator.typedefimplementation-definedconst_iterator;A constant iterator whose value type is value_type. The iterator category is at least a forward iterator.typedefimplementation-definedlocal_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.typedefimplementation-definedconst_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_set
public
construct/copy/destructexplicitunordered_set(size_type n =implementation-defined,hasherconst& hf =hasher(),key_equalconst& eq =key_equal(),allocator_typeconst& 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() == 0template<typename InputIterator>unordered_set(InputIterator f,InputIterator l,size_type n =implementation-defined,hasherconst& hf =hasher(),key_equalconst& eq =key_equal(),allocator_typeconst& 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.unordered_set(unordered_setconst&);The copy constructor. Copies the contained elements, hash function, predicate, maximum load factor and allocator.Requires:value_type is copy constructibleunordered_set(unordered_set&&);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).
explicitunordered_set(Allocatorconst& a);Constructs an empty container, using allocator a.unordered_set(unordered_setconst& x,Allocatorconst& a);Constructs an container, copying x's contained elements, hash function, predicate, maximum load factor, but using allocator a.~unordered_set();Notes:The destructor is applied to every element, and all memory is deallocatedunordered_set&operator=(unordered_setconst&);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_set)
in order to emulate move semantics.
Requires:value_type is copy constructibleunordered_set&operator=(unordered_set&&);The move assignment operator.Notes:
On compilers without rvalue references, there is a single assignment
operator with the signature operator=(unordered_set)
in order to emulate move semantics.
Requires:value_type is move constructible.
(TODO: This is not actually required in this implementation).
allocator_typeget_allocator()const;unordered_set size and capacityboolempty()const;Returns:size() == 0size_typesize()const;Returns:std::distance(begin(), end())size_typemax_size()const;Returns:size() of the largest possible container.
unordered_set iteratorsiteratorbegin();const_iteratorbegin()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.
iteratorend();const_iteratorend()const;Returns:An iterator which refers to the past-the-end value for the container.
const_iteratorcbegin()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.
const_iteratorcend()const;Returns:A constant iterator which refers to the past-the-end value for the container.
unordered_set modifierstemplate<typename... Args>std::pair<iterator,bool>emplace(Args&&... args);Inserts an object, constructed with the arguments args, in the container if and only if there is no element in the container with an equivalent value.Returns:The bool component of the return type is true if an insert took place.If an insert took place, then the iterator points to the newly inserted element. Otherwise, it points to the element with equivalent value.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.template<typename... Args>iteratoremplace_hint(const_iterator hint,Args&&... args);Inserts an object, constructed with the arguments args, in the container if and only if there is no element in the container with an equivalent value.hint is a suggestion to where the element should be inserted.Returns:If an insert took place, then the iterator points to the newly inserted element. Otherwise, it points to the element with equivalent value.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 value. 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.std::pair<iterator,bool>insert(value_typeconst& obj);Inserts obj in the container if and only if there is no element in the container with an equivalent value.Returns:The bool component of the return type is true if an insert took place.If an insert took place, then the iterator points to the newly inserted element. Otherwise, it points to the element with equivalent value.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.iteratorinsert(const_iterator hint,value_typeconst& obj);Inserts obj in the container if and only if there is no element in the container with an equivalent value.hint is a suggestion to where the element should be inserted.Returns:If an insert took place, then the iterator points to the newly inserted element. Otherwise, it points to the element with equivalent value.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 value. 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.template<typename InputIterator>voidinsert(InputIterator first,InputIterator last);Inserts a range of elements into the container. Elements are inserted if and only if there is no element in the container with an equivalent value.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.iteratorerase(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.Notes:
When the number of elements is a lot smaller than the number of buckets
this function can be very inefficient as it has to search through empty
buckets for the next element, in order to return the iterator.
The method quick_erase is faster, but has yet
to be standardized.
size_typeerase(key_typeconst& 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.iteratorerase(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.voidquick_erase(const_iterator position);Erase the element pointed to by position.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.Notes:
This method is faster than erase as
it doesn't have to find the next element in the container -
a potentially costly operation.
As it hasn't been standardized, it's likely that this may
change in the future.
voiderase_return_void(const_iterator position);Erase the element pointed to by position.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.Notes:
This method is now deprecated, use
quick_return instead. Although be
warned that as that isn't standardized yet, it could also
change.
voidclear();Erases all elements in the container.Postconditions:size() == 0Throws:Never throws an exception.voidswap(unordered_set&);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_set observershasherhash_function()const;Returns:The container's hash function.
key_equalkey_eq()const;Returns:The container's key equality predicate.
unordered_set lookupiteratorfind(key_typeconst& k);const_iteratorfind(key_typeconst& k)const;template<typename CompatibleKey,typename CompatibleHash,typename CompatiblePredicate>iteratorfind(CompatibleKeyconst& k,CompatibleHashconst& hash,CompatiblePredicateconst& eq);template<typename CompatibleKey,typename CompatibleHash,typename CompatiblePredicate>const_iteratorfind(CompatibleKeyconst& k,CompatibleHashconst& hash,CompatiblePredicateconst& eq)const;Returns:An iterator pointing to an element with key equivalent to k, or b.end() if no such element exists.Notes:
The templated overloads are a non-standard extensions which
allows you to use a compatible hash function and equality
predicate for a key of a different type in order to avoid
an expensive type cast. In general, its use is not encouraged.
size_typecount(key_typeconst& k)const;Returns:The number of elements with key equivalent to k.std::pair<iterator,iterator>equal_range(key_typeconst& k);std::pair<const_iterator,const_iterator>equal_range(key_typeconst& 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_set bucket interfacesize_typebucket_count()const;Returns:The number of buckets.size_typemax_bucket_count()const;Returns:An upper bound on the number of buckets.size_typebucket_size(size_type n)const;Requires:n < bucket_count()Returns:The number of elements in bucket n.size_typebucket(key_typeconst& 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()local_iteratorbegin(size_type n);const_local_iteratorbegin(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.local_iteratorend(size_type n);const_local_iteratorend(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.const_local_iteratorcbegin(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.const_local_iteratorcend(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_set hash policyfloatload_factor()const;Returns:The average number of elements per bucket.floatmax_load_factor()const;Returns:Returns the current maximum load factor.voidmax_load_factor(float z);Effects:Changes the container's maximum load factor, using z as a hint.voidrehash(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_set Equality Comparisonstemplate<typename Value,typename Hash,typename Pred,typename Alloc>booloperator==(unordered_set<Value,Hash,Pred,Alloc>const& x,unordered_set<Value,Hash,Pred,Alloc>const& y);Notes:This is a boost extension.Behavior is undefined if the two containers don't have
equivalent equality predicates.template<typename Value,typename Hash,typename Pred,typename Alloc>booloperator!=(unordered_set<Value,Hash,Pred,Alloc>const& x,unordered_set<Value,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_set swaptemplate<typename Value,typename Hash,typename Pred,typename Alloc>voidswap(unordered_set<Value,Hash,Pred,Alloc>& x,unordered_set<Value,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.Class template unordered_multiset3boost::unordered_multiset
An unordered associative container that stores values. The same key can be stored multiple times.
// In header: <boost/unordered_set.hpp>
template<typename Value,typename Hash =boost::hash<Value>,typename Pred =std::equal_to<Value>,typename Alloc =std::allocator<Value>>class unordered_multiset {public:// typestypedefValuekey_type;typedefValuevalue_type;typedefHashhasher;typedefPredkey_equal;typedefAllocallocator_type;typedeftypenameallocator_type::pointerpointer;typedeftypenameallocator_type::const_pointerconst_pointer;typedeftypenameallocator_type::referencereference;// lvalue of value_type.typedeftypenameallocator_type::const_referenceconst_reference;// const lvalue of value_type.typedefimplementation-definedsize_type;typedefimplementation-defineddifference_type;typedefimplementation-definediterator;typedefimplementation-definedconst_iterator;typedefimplementation-definedlocal_iterator;typedefimplementation-definedconst_local_iterator;// construct/copy/destructexplicitunordered_multiset(size_type=implementation-defined,hasherconst&=hasher(),key_equalconst&=key_equal(),allocator_typeconst&=allocator_type());template<typename InputIterator>unordered_multiset(InputIterator,InputIterator,size_type=implementation-defined,hasherconst&=hasher(),key_equalconst&=key_equal(),allocator_typeconst&=allocator_type());unordered_multiset(unordered_multisetconst&);unordered_multiset(unordered_multiset&&);explicitunordered_multiset(Allocatorconst&);unordered_multiset(unordered_multisetconst&,Allocatorconst&);~unordered_multiset();unordered_multiset&operator=(unordered_multisetconst&);unordered_multiset&operator=(unordered_multiset&&);allocator_typeget_allocator()const;// size and capacityboolempty()const;size_typesize()const;size_typemax_size()const;// iteratorsiteratorbegin();const_iteratorbegin()const;iteratorend();const_iteratorend()const;const_iteratorcbegin()const;const_iteratorcend()const;// modifierstemplate<typename... Args>iteratoremplace(Args&&...);template<typename... Args>iteratoremplace_hint(const_iterator,Args&&...);iteratorinsert(value_typeconst&);iteratorinsert(const_iterator,value_typeconst&);template<typename InputIterator>voidinsert(InputIterator,InputIterator);iteratorerase(const_iterator);size_typeerase(key_typeconst&);iteratorerase(const_iterator,const_iterator);voidquick_erase(const_iterator);voiderase_return_void(const_iterator);voidclear();voidswap(unordered_multiset&);// observershasherhash_function()const;key_equalkey_eq()const;// lookupiteratorfind(key_typeconst&);const_iteratorfind(key_typeconst&)const;template<typename CompatibleKey,typename CompatibleHash,typename CompatiblePredicate>iteratorfind(CompatibleKeyconst&,CompatibleHashconst&,CompatiblePredicateconst&);template<typename CompatibleKey,typename CompatibleHash,typename CompatiblePredicate>const_iteratorfind(CompatibleKeyconst&,CompatibleHashconst&,CompatiblePredicateconst&)const;size_typecount(key_typeconst&)const;std::pair<iterator,iterator>equal_range(key_typeconst&);std::pair<const_iterator,const_iterator>equal_range(key_typeconst&)const;// bucket interfacesize_typebucket_count()const;size_typemax_bucket_count()const;size_typebucket_size(size_type)const;size_typebucket(key_typeconst&)const;local_iteratorbegin(size_type);const_local_iteratorbegin(size_type)const;local_iteratorend(size_type);const_local_iteratorend(size_type)const;const_local_iteratorcbegin(size_type)const;const_local_iteratorcend(size_type);// hash policyfloatload_factor()const;floatmax_load_factor()const;voidmax_load_factor(float);voidrehash(size_type);};// Equality Comparisonstemplate<typename Value,typename Hash,typename Pred,typename Alloc>booloperator==(unordered_multiset<Value,Hash,Pred,Alloc>const&,unordered_multiset<Value,Hash,Pred,Alloc>const&);template<typename Value,typename Hash,typename Pred,typename Alloc>booloperator!=(unordered_multiset<Value,Hash,Pred,Alloc>const&,unordered_multiset<Value,Hash,Pred,Alloc>const&);// swaptemplate<typename Value,typename Hash,typename Pred,typename Alloc>voidswap(unordered_multiset<Value,Hash,Pred,Alloc>&,unordered_multiset<Value,Hash,Pred,Alloc>&);DescriptionBased on chapter 23 of
the working draft of the C++ standard [n2960].
But without the updated rules for allocators.
Template ParametersValueValue must be Assignable and CopyConstructibleHashA unary function object type that acts a hash function for a Value. It takes a single argument of type Value and returns a value of type std::size_t.PredA binary function object that implements an equivalence relation on values of type Value.
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.AllocAn 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_multiset
public
typestypedefimplementation-definedsize_type;An unsigned integral type.size_type can represent any non-negative value of difference_type.typedefimplementation-defineddifference_type;A signed integral type.Is identical to the difference type of iterator and const_iterator.typedefimplementation-definediterator;A constant iterator whose value type is value_type. The iterator category is at least a forward iterator.Convertible to const_iterator.typedefimplementation-definedconst_iterator;A constant iterator whose value type is value_type. The iterator category is at least a forward iterator.typedefimplementation-definedlocal_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.typedefimplementation-definedconst_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_multiset
public
construct/copy/destructexplicitunordered_multiset(size_type n =implementation-defined,hasherconst& hf =hasher(),key_equalconst& eq =key_equal(),allocator_typeconst& 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() == 0template<typename InputIterator>unordered_multiset(InputIterator f,InputIterator l,size_type n =implementation-defined,hasherconst& hf =hasher(),key_equalconst& eq =key_equal(),allocator_typeconst& 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.unordered_multiset(unordered_multisetconst&);The copy constructor. Copies the contained elements, hash function, predicate, maximum load factor and allocator.Requires:value_type is copy constructibleunordered_multiset(unordered_multiset&&);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).
explicitunordered_multiset(Allocatorconst& a);Constructs an empty container, using allocator a.unordered_multiset(unordered_multisetconst& x,Allocatorconst& a);Constructs an container, copying x's contained elements, hash function, predicate, maximum load factor, but using allocator a.~unordered_multiset();Notes:The destructor is applied to every element, and all memory is deallocatedunordered_multiset&operator=(unordered_multisetconst&);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_multiset)
in order to emulate move semantics.
Requires:value_type is copy constructibleunordered_multiset&operator=(unordered_multiset&&);The move assignment operator.Notes:
On compilers without rvalue references, there is a single assignment
operator with the signature operator=(unordered_multiset)
in order to emulate move semantics.
Requires:value_type is move constructible.
(TODO: This is not actually required in this implementation).
allocator_typeget_allocator()const;unordered_multiset size and capacityboolempty()const;Returns:size() == 0size_typesize()const;Returns:std::distance(begin(), end())size_typemax_size()const;Returns:size() of the largest possible container.
unordered_multiset iteratorsiteratorbegin();const_iteratorbegin()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.
iteratorend();const_iteratorend()const;Returns:An iterator which refers to the past-the-end value for the container.
const_iteratorcbegin()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.
const_iteratorcend()const;Returns:A constant iterator which refers to the past-the-end value for the container.
unordered_multiset modifierstemplate<typename... Args>iteratoremplace(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.template<typename... Args>iteratoremplace_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 value. 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.iteratorinsert(value_typeconst& 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.iteratorinsert(const_iterator hint,value_typeconst& 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 value. 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.template<typename InputIterator>voidinsert(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.iteratorerase(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.Notes:
When the number of elements is a lot smaller than the number of buckets
this function can be very inefficient as it has to search through empty
buckets for the next element, in order to return the iterator.
The method quick_erase is faster, but has yet
to be standardized.
size_typeerase(key_typeconst& 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.iteratorerase(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.voidquick_erase(const_iterator position);Erase the element pointed to by position.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.Notes:
This method is faster than erase as
it doesn't have to find the next element in the container -
a potentially costly operation.
As it hasn't been standardized, it's likely that this may
change in the future.
voiderase_return_void(const_iterator position);Erase the element pointed to by position.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.Notes:
This method is now deprecated, use
quick_return instead. Although be
warned that as that isn't standardized yet, it could also
change.
voidclear();Erases all elements in the container.Postconditions:size() == 0Throws:Never throws an exception.voidswap(unordered_multiset&);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_multiset observershasherhash_function()const;Returns:The container's hash function.
key_equalkey_eq()const;Returns:The container's key equality predicate.
unordered_multiset lookupiteratorfind(key_typeconst& k);const_iteratorfind(key_typeconst& k)const;template<typename CompatibleKey,typename CompatibleHash,typename CompatiblePredicate>iteratorfind(CompatibleKeyconst& k,CompatibleHashconst& hash,CompatiblePredicateconst& eq);template<typename CompatibleKey,typename CompatibleHash,typename CompatiblePredicate>const_iteratorfind(CompatibleKeyconst& k,CompatibleHashconst& hash,CompatiblePredicateconst& eq)const;Returns:An iterator pointing to an element with key equivalent to k, or b.end() if no such element exists.Notes:
The templated overloads are a non-standard extensions which
allows you to use a compatible hash function and equality
predicate for a key of a different type in order to avoid
an expensive type cast. In general, its use is not encouraged.
size_typecount(key_typeconst& k)const;Returns:The number of elements with key equivalent to k.std::pair<iterator,iterator>equal_range(key_typeconst& k);std::pair<const_iterator,const_iterator>equal_range(key_typeconst& 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_multiset bucket interfacesize_typebucket_count()const;Returns:The number of buckets.size_typemax_bucket_count()const;Returns:An upper bound on the number of buckets.size_typebucket_size(size_type n)const;Requires:n < bucket_count()Returns:The number of elements in bucket n.size_typebucket(key_typeconst& 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()local_iteratorbegin(size_type n);const_local_iteratorbegin(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.local_iteratorend(size_type n);const_local_iteratorend(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.const_local_iteratorcbegin(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.const_local_iteratorcend(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_multiset hash policyfloatload_factor()const;Returns:The average number of elements per bucket.floatmax_load_factor()const;Returns:Returns the current maximum load factor.voidmax_load_factor(float z);Effects:Changes the container's maximum load factor, using z as a hint.voidrehash(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_multiset Equality Comparisonstemplate<typename Value,typename Hash,typename Pred,typename Alloc>booloperator==(unordered_multiset<Value,Hash,Pred,Alloc>const& x,unordered_multiset<Value,Hash,Pred,Alloc>const& y);Notes:This is a boost extension.Behavior is undefined if the two containers don't have
equivalent equality predicates.template<typename Value,typename Hash,typename Pred,typename Alloc>booloperator!=(unordered_multiset<Value,Hash,Pred,Alloc>const& x,unordered_multiset<Value,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_multiset swaptemplate<typename Value,typename Hash,typename Pred,typename Alloc>voidswap(unordered_multiset<Value,Hash,Pred,Alloc>& x,unordered_multiset<Value,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.Header <boost/unordered_map.hpp>namespaceboost{template<typename Key,typename Mapped,typename Hash =boost::hash<Key>,typename Pred =std::equal_to<Key>,typename Alloc =std::allocator<std::pair<Keyconst,Mapped>>>class unordered_map;template<typename Key,typename Mapped,typename Hash,typename Pred,typename Alloc>booloperator==(unordered_map<Key,Mapped,Hash,Pred,Alloc>const&,unordered_map<Key,Mapped,Hash,Pred,Alloc>const&);template<typename Key,typename Mapped,typename Hash,typename Pred,typename Alloc>booloperator!=(unordered_map<Key,Mapped,Hash,Pred,Alloc>const&,unordered_map<Key,Mapped,Hash,Pred,Alloc>const&);template<typename Key,typename Mapped,typename Hash,typename Pred,typename Alloc>voidswap(unordered_map<Key,Mapped,Hash,Pred,Alloc>&,unordered_map<Key,Mapped,Hash,Pred,Alloc>&);template<typename Key,typename Mapped,typename Hash =boost::hash<Key>,typename Pred =std::equal_to<Key>,typename Alloc =std::allocator<std::pair<Keyconst,Mapped>>>class unordered_multimap;template<typename Key,typename Mapped,typename Hash,typename Pred,typename Alloc>booloperator==(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>booloperator!=(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>voidswap(unordered_multimap<Key,Mapped,Hash,Pred,Alloc>&,unordered_multimap<Key,Mapped,Hash,Pred,Alloc>&);}Class template unordered_map3boost::unordered_map
An unordered associative container that associates unique keys with another value.
// 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<Keyconst,Mapped>>>class unordered_map {public:// typestypedefKeykey_type;typedefstd::pair<Keyconst,Mapped>value_type;typedefMappedmapped_type;typedefHashhasher;typedefPredkey_equal;typedefAllocallocator_type;typedeftypenameallocator_type::pointerpointer;typedeftypenameallocator_type::const_pointerconst_pointer;typedeftypenameallocator_type::referencereference;// lvalue of value_type.typedeftypenameallocator_type::const_referenceconst_reference;// const lvalue of value_type.typedefimplementation-definedsize_type;typedefimplementation-defineddifference_type;typedefimplementation-definediterator;typedefimplementation-definedconst_iterator;typedefimplementation-definedlocal_iterator;typedefimplementation-definedconst_local_iterator;// construct/copy/destructexplicitunordered_map(size_type=implementation-defined,hasherconst&=hasher(),key_equalconst&=key_equal(),allocator_typeconst&=allocator_type());template<typename InputIterator>unordered_map(InputIterator,InputIterator,size_type=implementation-defined,hasherconst&=hasher(),key_equalconst&=key_equal(),allocator_typeconst&=allocator_type());unordered_map(unordered_mapconst&);unordered_map(unordered_map&&);explicitunordered_map(Allocatorconst&);unordered_map(unordered_mapconst&,Allocatorconst&);~unordered_map();unordered_map&operator=(unordered_mapconst&);unordered_map&operator=(unordered_map&&);allocator_typeget_allocator()const;// size and capacityboolempty()const;size_typesize()const;size_typemax_size()const;// iteratorsiteratorbegin();const_iteratorbegin()const;iteratorend();const_iteratorend()const;const_iteratorcbegin()const;const_iteratorcend()const;// modifierstemplate<typename... Args>std::pair<iterator,bool>emplace(Args&&...);template<typename... Args>iteratoremplace_hint(const_iterator,Args&&...);std::pair<iterator,bool>insert(value_typeconst&);iteratorinsert(const_iterator,value_typeconst&);template<typename InputIterator>voidinsert(InputIterator,InputIterator);iteratorerase(const_iterator);size_typeerase(key_typeconst&);iteratorerase(const_iterator,const_iterator);voidquick_erase(const_iterator);voiderase_return_void(const_iterator);voidclear();voidswap(unordered_map&);// observershasherhash_function()const;key_equalkey_eq()const;// lookupiteratorfind(key_typeconst&);const_iteratorfind(key_typeconst&)const;template<typename CompatibleKey,typename CompatibleHash,typename CompatiblePredicate>iteratorfind(CompatibleKeyconst&,CompatibleHashconst&,CompatiblePredicateconst&);template<typename CompatibleKey,typename CompatibleHash,typename CompatiblePredicate>const_iteratorfind(CompatibleKeyconst&,CompatibleHashconst&,CompatiblePredicateconst&)const;size_typecount(key_typeconst&)const;std::pair<iterator,iterator>equal_range(key_typeconst&);std::pair<const_iterator,const_iterator>equal_range(key_typeconst&)const;mapped_type&operator[](key_typeconst&);Mapped&at(key_typeconst&);Mappedconst&at(key_typeconst&)const;// bucket interfacesize_typebucket_count()const;size_typemax_bucket_count()const;size_typebucket_size(size_type)const;size_typebucket(key_typeconst&)const;local_iteratorbegin(size_type);const_local_iteratorbegin(size_type)const;local_iteratorend(size_type);const_local_iteratorend(size_type)const;const_local_iteratorcbegin(size_type)const;const_local_iteratorcend(size_type);// hash policyfloatload_factor()const;floatmax_load_factor()const;voidmax_load_factor(float);voidrehash(size_type);};// Equality Comparisonstemplate<typename Key,typename Mapped,typename Hash,typename Pred,typename Alloc>booloperator==(unordered_map<Key,Mapped,Hash,Pred,Alloc>const&,unordered_map<Key,Mapped,Hash,Pred,Alloc>const&);template<typename Key,typename Mapped,typename Hash,typename Pred,typename Alloc>booloperator!=(unordered_map<Key,Mapped,Hash,Pred,Alloc>const&,unordered_map<Key,Mapped,Hash,Pred,Alloc>const&);// swaptemplate<typename Key,typename Mapped,typename Hash,typename Pred,typename Alloc>voidswap(unordered_map<Key,Mapped,Hash,Pred,Alloc>&,unordered_map<Key,Mapped,Hash,Pred,Alloc>&);DescriptionBased on chapter 23 of
the working draft of the C++ standard [n2960].
But without the updated rules for allocators.
Template ParametersKeyKey must be Assignable and CopyConstructible.MappedMapped must be CopyConstructibleHashA 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.PredA 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.AllocAn 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.The number of buckets can be automatically increased by a call to insert, or as the result of calling rehash.unordered_map
public
typestypedefimplementation-definedsize_type;An unsigned integral type.size_type can represent any non-negative value of difference_type.typedefimplementation-defineddifference_type;A signed integral type.Is identical to the difference type of iterator and const_iterator.typedefimplementation-definediterator;A iterator whose value type is value_type. The iterator category is at least a forward iterator.Convertible to const_iterator.typedefimplementation-definedconst_iterator;A constant iterator whose value type is value_type. The iterator category is at least a forward iterator.typedefimplementation-definedlocal_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.typedefimplementation-definedconst_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_map
public
construct/copy/destructexplicitunordered_map(size_type n =implementation-defined,hasherconst& hf =hasher(),key_equalconst& eq =key_equal(),allocator_typeconst& 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() == 0template<typename InputIterator>unordered_map(InputIterator f,InputIterator l,size_type n =implementation-defined,hasherconst& hf =hasher(),key_equalconst& eq =key_equal(),allocator_typeconst& 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.unordered_map(unordered_mapconst&);The copy constructor. Copies the contained elements, hash function, predicate, maximum load factor and allocator.Requires:value_type is copy constructibleunordered_map(unordered_map&&);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).
explicitunordered_map(Allocatorconst& a);Constructs an empty container, using allocator a.unordered_map(unordered_mapconst& x,Allocatorconst& a);Constructs an container, copying x's contained elements, hash function, predicate, maximum load factor, but using allocator a.~unordered_map();Notes:The destructor is applied to every element, and all memory is deallocatedunordered_map&operator=(unordered_mapconst&);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_map)
in order to emulate move semantics.
Requires:value_type is copy constructibleunordered_map&operator=(unordered_map&&);The move assignment operator.Notes:
On compilers without rvalue references, there is a single assignment
operator with the signature operator=(unordered_map)
in order to emulate move semantics.
Requires:value_type is move constructible.
(TODO: This is not actually required in this implementation).
allocator_typeget_allocator()const;unordered_map size and capacityboolempty()const;Returns:size() == 0size_typesize()const;Returns:std::distance(begin(), end())size_typemax_size()const;Returns:size() of the largest possible container.
unordered_map iteratorsiteratorbegin();const_iteratorbegin()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.
iteratorend();const_iteratorend()const;Returns:An iterator which refers to the past-the-end value for the container.
const_iteratorcbegin()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.
const_iteratorcend()const;Returns:A constant iterator which refers to the past-the-end value for the container.
unordered_map modifierstemplate<typename... Args>std::pair<iterator,bool>emplace(Args&&... args);Inserts an object, constructed with the arguments args, in the container if and only if there is no element in the container with an equivalent key.Returns:The bool component of the return type is true if an insert took place.If an insert took place, then the iterator points to the newly inserted element. Otherwise, it points to the element with equivalent key.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.template<typename... Args>iteratoremplace_hint(const_iterator hint,Args&&... args);Inserts an object, constructed with the arguments args, in the container if and only if there is no element in the container with an equivalent key.hint is a suggestion to where the element should be inserted.Returns:If an insert took place, then the iterator points to the newly inserted element. Otherwise, it points to the element with equivalent key.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.std::pair<iterator,bool>insert(value_typeconst& obj);Inserts obj in the container if and only if there is no element in the container with an equivalent key.Returns:The bool component of the return type is true if an insert took place.If an insert took place, then the iterator points to the newly inserted element. Otherwise, it points to the element with equivalent key.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.iteratorinsert(const_iterator hint,value_typeconst& obj);Inserts obj in the container if and only if there is no element in the container with an equivalent key.hint is a suggestion to where the element should be inserted.Returns:If an insert took place, then the iterator points to the newly inserted element. Otherwise, it points to the element with equivalent key.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.template<typename InputIterator>voidinsert(InputIterator first,InputIterator last);Inserts a range of elements into the container. Elements are inserted if and only if there is no element in the container with an equivalent key.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.iteratorerase(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.Notes:
When the number of elements is a lot smaller than the number of buckets
this function can be very inefficient as it has to search through empty
buckets for the next element, in order to return the iterator.
The method quick_erase is faster, but has yet
to be standardized.
size_typeerase(key_typeconst& 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.iteratorerase(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.voidquick_erase(const_iterator position);Erase the element pointed to by position.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.Notes:
This method is faster than erase as
it doesn't have to find the next element in the container -
a potentially costly operation.
As it hasn't been standardized, it's likely that this may
change in the future.
voiderase_return_void(const_iterator position);Erase the element pointed to by position.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.Notes:
This method is now deprecated, use
quick_return instead. Although be
warned that as that isn't standardized yet, it could also
change.
voidclear();Erases all elements in the container.Postconditions:size() == 0Throws:Never throws an exception.voidswap(unordered_map&);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_map observershasherhash_function()const;Returns:The container's hash function.
key_equalkey_eq()const;Returns:The container's key equality predicate.
unordered_map lookupiteratorfind(key_typeconst& k);const_iteratorfind(key_typeconst& k)const;template<typename CompatibleKey,typename CompatibleHash,typename CompatiblePredicate>iteratorfind(CompatibleKeyconst& k,CompatibleHashconst& hash,CompatiblePredicateconst& eq);template<typename CompatibleKey,typename CompatibleHash,typename CompatiblePredicate>const_iteratorfind(CompatibleKeyconst& k,CompatibleHashconst& hash,CompatiblePredicateconst& eq)const;Returns:An iterator pointing to an element with key equivalent to k, or b.end() if no such element exists.Notes:
The templated overloads are a non-standard extensions which
allows you to use a compatible hash function and equality
predicate for a key of a different type in order to avoid
an expensive type cast. In general, its use is not encouraged.
size_typecount(key_typeconst& k)const;Returns:The number of elements with key equivalent to k.std::pair<iterator,iterator>equal_range(key_typeconst& k);std::pair<const_iterator,const_iterator>equal_range(key_typeconst& 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()).
mapped_type&operator[](key_typeconst& k);Effects:If the container does not already contain an elements with a key equivalent to k, inserts the value std::pair<key_type const, mapped_type>(k, mapped_type())Returns:A reference to x.second where x is the element already in the container, or the newly inserted element with a key equivalent to kThrows: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.Mapped&at(key_typeconst& k);Mappedconst&at(key_typeconst& k)const;Returns:A reference to x.second where x is the (unique) element whose key is equivalent to k.Throws:An exception object of type std::out_of_range if no such element is present.Notes:This is not specified in the draft standard, but that is probably an oversight. The issue has been raised in
comp.std.c++.unordered_map bucket interfacesize_typebucket_count()const;Returns:The number of buckets.size_typemax_bucket_count()const;Returns:An upper bound on the number of buckets.size_typebucket_size(size_type n)const;Requires:n < bucket_count()Returns:The number of elements in bucket n.size_typebucket(key_typeconst& 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()local_iteratorbegin(size_type n);const_local_iteratorbegin(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.local_iteratorend(size_type n);const_local_iteratorend(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.const_local_iteratorcbegin(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.const_local_iteratorcend(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_map hash policyfloatload_factor()const;Returns:The average number of elements per bucket.floatmax_load_factor()const;Returns:Returns the current maximum load factor.voidmax_load_factor(float z);Effects:Changes the container's maximum load factor, using z as a hint.voidrehash(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_map Equality Comparisonstemplate<typename Key,typename Mapped,typename Hash,typename Pred,typename Alloc>booloperator==(unordered_map<Key,Mapped,Hash,Pred,Alloc>const& x,unordered_map<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.template<typename Key,typename Mapped,typename Hash,typename Pred,typename Alloc>booloperator!=(unordered_map<Key,Mapped,Hash,Pred,Alloc>const& x,unordered_map<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_map swaptemplate<typename Key,typename Mapped,typename Hash,typename Pred,typename Alloc>voidswap(unordered_map<Key,Mapped,Hash,Pred,Alloc>& x,unordered_map<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.Class template unordered_multimap3boost::unordered_multimap
An unordered associative container that associates keys with another value. The same key can be stored multiple times.
// 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<Keyconst,Mapped>>>class unordered_multimap {public:// typestypedefKeykey_type;typedefstd::pair<Keyconst,Mapped>value_type;typedefMappedmapped_type;typedefHashhasher;typedefPredkey_equal;typedefAllocallocator_type;typedeftypenameallocator_type::pointerpointer;typedeftypenameallocator_type::const_pointerconst_pointer;typedeftypenameallocator_type::referencereference;// lvalue of value_type.typedeftypenameallocator_type::const_referenceconst_reference;// const lvalue of value_type.typedefimplementation-definedsize_type;typedefimplementation-defineddifference_type;typedefimplementation-definediterator;typedefimplementation-definedconst_iterator;typedefimplementation-definedlocal_iterator;typedefimplementation-definedconst_local_iterator;// construct/copy/destructexplicitunordered_multimap(size_type=implementation-defined,hasherconst&=hasher(),key_equalconst&=key_equal(),allocator_typeconst&=allocator_type());template<typename InputIterator>unordered_multimap(InputIterator,InputIterator,size_type=implementation-defined,hasherconst&=hasher(),key_equalconst&=key_equal(),allocator_typeconst&=allocator_type());unordered_multimap(unordered_multimapconst&);unordered_multimap(unordered_multimap&&);explicitunordered_multimap(Allocatorconst&);unordered_multimap(unordered_multimapconst&,Allocatorconst&);~unordered_multimap();unordered_multimap&operator=(unordered_multimapconst&);unordered_multimap&operator=(unordered_multimap&&);allocator_typeget_allocator()const;// size and capacityboolempty()const;size_typesize()const;size_typemax_size()const;// iteratorsiteratorbegin();const_iteratorbegin()const;iteratorend();const_iteratorend()const;const_iteratorcbegin()const;const_iteratorcend()const;// modifierstemplate<typename... Args>iteratoremplace(Args&&...);template<typename... Args>iteratoremplace_hint(const_iterator,Args&&...);iteratorinsert(value_typeconst&);iteratorinsert(const_iterator,value_typeconst&);template<typename InputIterator>voidinsert(InputIterator,InputIterator);iteratorerase(const_iterator);size_typeerase(key_typeconst&);iteratorerase(const_iterator,const_iterator);voidquick_erase(const_iterator);voiderase_return_void(const_iterator);voidclear();voidswap(unordered_multimap&);// observershasherhash_function()const;key_equalkey_eq()const;// lookupiteratorfind(key_typeconst&);const_iteratorfind(key_typeconst&)const;template<typename CompatibleKey,typename CompatibleHash,typename CompatiblePredicate>iteratorfind(CompatibleKeyconst&,CompatibleHashconst&,CompatiblePredicateconst&);template<typename CompatibleKey,typename CompatibleHash,typename CompatiblePredicate>const_iteratorfind(CompatibleKeyconst&,CompatibleHashconst&,CompatiblePredicateconst&)const;size_typecount(key_typeconst&)const;std::pair<iterator,iterator>equal_range(key_typeconst&);std::pair<const_iterator,const_iterator>equal_range(key_typeconst&)const;// bucket interfacesize_typebucket_count()const;size_typemax_bucket_count()const;size_typebucket_size(size_type)const;size_typebucket(key_typeconst&)const;local_iteratorbegin(size_type);const_local_iteratorbegin(size_type)const;local_iteratorend(size_type);const_local_iteratorend(size_type)const;const_local_iteratorcbegin(size_type)const;const_local_iteratorcend(size_type);// hash policyfloatload_factor()const;floatmax_load_factor()const;voidmax_load_factor(float);voidrehash(size_type);};// Equality Comparisonstemplate<typename Key,typename Mapped,typename Hash,typename Pred,typename Alloc>booloperator==(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>booloperator!=(unordered_multimap<Key,Mapped,Hash,Pred,Alloc>const&,unordered_multimap<Key,Mapped,Hash,Pred,Alloc>const&);// swaptemplate<typename Key,typename Mapped,typename Hash,typename Pred,typename Alloc>voidswap(unordered_multimap<Key,Mapped,Hash,Pred,Alloc>&,unordered_multimap<Key,Mapped,Hash,Pred,Alloc>&);DescriptionBased on chapter 23 of
the working draft of the C++ standard [n2960].
But without the updated rules for allocators.
Template ParametersKeyKey must be Assignable and CopyConstructible.MappedMapped must be CopyConstructibleHashA 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.PredA 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.AllocAn 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
typestypedefimplementation-definedsize_type;An unsigned integral type.size_type can represent any non-negative value of difference_type.typedefimplementation-defineddifference_type;A signed integral type.Is identical to the difference type of iterator and const_iterator.typedefimplementation-definediterator;A iterator whose value type is value_type. The iterator category is at least a forward iterator.Convertible to const_iterator.typedefimplementation-definedconst_iterator;A constant iterator whose value type is value_type. The iterator category is at least a forward iterator.typedefimplementation-definedlocal_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.typedefimplementation-definedconst_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/destructexplicitunordered_multimap(size_type n =implementation-defined,hasherconst& hf =hasher(),key_equalconst& eq =key_equal(),allocator_typeconst& 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() == 0template<typename InputIterator>unordered_multimap(InputIterator f,InputIterator l,size_type n =implementation-defined,hasherconst& hf =hasher(),key_equalconst& eq =key_equal(),allocator_typeconst& 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.unordered_multimap(unordered_multimapconst&);The copy constructor. Copies the contained elements, hash function, predicate, maximum load factor and allocator.Requires:value_type is copy constructibleunordered_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).
explicitunordered_multimap(Allocatorconst& a);Constructs an empty container, using allocator a.unordered_multimap(unordered_multimapconst& x,Allocatorconst& a);Constructs an container, copying x's contained elements, hash function, predicate, maximum load factor, but using allocator a.~unordered_multimap();Notes:The destructor is applied to every element, and all memory is deallocatedunordered_multimap&operator=(unordered_multimapconst&);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 constructibleunordered_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_typeget_allocator()const;unordered_multimap size and capacityboolempty()const;Returns:size() == 0size_typesize()const;Returns:std::distance(begin(), end())size_typemax_size()const;Returns:size() of the largest possible container.
unordered_multimap iteratorsiteratorbegin();const_iteratorbegin()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.
iteratorend();const_iteratorend()const;Returns:An iterator which refers to the past-the-end value for the container.
const_iteratorcbegin()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.
const_iteratorcend()const;Returns:A constant iterator which refers to the past-the-end value for the container.
unordered_multimap modifierstemplate<typename... Args>iteratoremplace(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.template<typename... Args>iteratoremplace_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.iteratorinsert(value_typeconst& 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.iteratorinsert(const_iterator hint,value_typeconst& 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.template<typename InputIterator>voidinsert(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.iteratorerase(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.Notes:
When the number of elements is a lot smaller than the number of buckets
this function can be very inefficient as it has to search through empty
buckets for the next element, in order to return the iterator.
The method quick_erase is faster, but has yet
to be standardized.
size_typeerase(key_typeconst& 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.iteratorerase(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.voidquick_erase(const_iterator position);Erase the element pointed to by position.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.Notes:
This method is faster than erase as
it doesn't have to find the next element in the container -
a potentially costly operation.
As it hasn't been standardized, it's likely that this may
change in the future.
voiderase_return_void(const_iterator position);Erase the element pointed to by position.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.Notes:
This method is now deprecated, use
quick_return instead. Although be
warned that as that isn't standardized yet, it could also
change.
voidclear();Erases all elements in the container.Postconditions:size() == 0Throws:Never throws an exception.voidswap(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 observershasherhash_function()const;Returns:The container's hash function.
key_equalkey_eq()const;Returns:The container's key equality predicate.
unordered_multimap lookupiteratorfind(key_typeconst& k);const_iteratorfind(key_typeconst& k)const;template<typename CompatibleKey,typename CompatibleHash,typename CompatiblePredicate>iteratorfind(CompatibleKeyconst& k,CompatibleHashconst& hash,CompatiblePredicateconst& eq);template<typename CompatibleKey,typename CompatibleHash,typename CompatiblePredicate>const_iteratorfind(CompatibleKeyconst& k,CompatibleHashconst& hash,CompatiblePredicateconst& eq)const;Returns:An iterator pointing to an element with key equivalent to k, or b.end() if no such element exists.Notes:
The templated overloads are a non-standard extensions which
allows you to use a compatible hash function and equality
predicate for a key of a different type in order to avoid
an expensive type cast. In general, its use is not encouraged.
size_typecount(key_typeconst& k)const;Returns:The number of elements with key equivalent to k.std::pair<iterator,iterator>equal_range(key_typeconst& k);std::pair<const_iterator,const_iterator>equal_range(key_typeconst& 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 interfacesize_typebucket_count()const;Returns:The number of buckets.size_typemax_bucket_count()const;Returns:An upper bound on the number of buckets.size_typebucket_size(size_type n)const;Requires:n < bucket_count()Returns:The number of elements in bucket n.size_typebucket(key_typeconst& 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()local_iteratorbegin(size_type n);const_local_iteratorbegin(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.local_iteratorend(size_type n);const_local_iteratorend(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.const_local_iteratorcbegin(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.const_local_iteratorcend(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 policyfloatload_factor()const;Returns:The average number of elements per bucket.floatmax_load_factor()const;Returns:Returns the current maximum load factor.voidmax_load_factor(float z);Effects:Changes the container's maximum load factor, using z as a hint.voidrehash(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 Comparisonstemplate<typename Key,typename Mapped,typename Hash,typename Pred,typename Alloc>booloperator==(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.template<typename Key,typename Mapped,typename Hash,typename Pred,typename Alloc>booloperator!=(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 swaptemplate<typename Key,typename Mapped,typename Hash,typename Pred,typename Alloc>voidswap(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.