<unordered_set>

[added with C++11]


Include the STL standard header <unordered_set> to define the container template classes unordered_set and unordered_multiset, and their supporting templates.

namespace std {
        // DECLARATIONS
template<class Key, class Hash, class Pred, class Alloc>
    class unordered_set;
template<class Key, class Hash, class Pred, class Alloc>
    class unordered_multiset;

        // TEMPLATE FUNCTIONS
template<class Key, class Hash, class Pred, class Alloc>
    void swap(
        unordered_set<Key, Hash, Pred, Alloc>& left,
        unordered_set<Key, Hash, Pred, Alloc>& right);
template<class Key, class Hash, class Pred, class Alloc>
    void swap(
        unordered_multiset<Key, Hash, Pred, Alloc>& left,
        unordered_multiset<Key, Hash, Pred, Alloc>& right);

        // TEMPLATE OPERATORS
template<class Key, class Hash, class Pred, class Alloc>
    bool operator==( [added with C++11]
        const unordered_set<Key, Hash, Pred, Alloc>& left,
        const unordered_set<Key, Hash, Pred, Alloc>& right);
template<class Key, class Hash, class Pred, class Alloc>
    bool operator==( [added with C++11]
        const unordered_multiset<Key, Hash, Pred, Alloc>& left,
        const unordered_multiset<Key, Hash, Pred, Alloc>& right);

template<class Key, class Hash, class Pred, class Alloc>
    bool operator!=( [added with C++11]
        const unordered_set<Key, Hash, Pred, Alloc>& left,
        const unordered_set<Key, Hash, Pred, Alloc>& right);
template<class Key, class Hash, class Pred, class Alloc>
    bool operator!=( [added with C++11]
        const unordered_multiset<Key, Hash, Pred, Alloc>& left,
        const unordered_multiset<Key, Hash, Pred, Alloc>& right);
    namespace tr1 {
using std::unordered_multiset; using std::unordered_set; [added with C++11]
    }  // namespace tr1
}  // namespace std

operator==

template<class Key, class Hash, class Pred, class Alloc>
    bool operator==( [added with C++11]
        const unordered_set<Key, Hash, Pred, Alloc>& left,
        const unordered_set<Key, Hash, Pred, Alloc>& right);
template<class Key, class Hash, class Pred, class Alloc>
    bool operator==( [added with C++11]
        const unordered_multiset<Key, Hash, Pred, Alloc>& left,
        const unordered_multiset<Key, Hash, Pred, Alloc>& right);

The first template function overloads operator== to compare two objects of template class unordered_set. The function returns true only if left.size() == right.size() and, for each element X in left, right.find(X) exists and compares equal to X using operator==.

The second template function overloads operator== to compare two objects of template class unordered_multiset. The function returns true only if left.size() == right.size() and, for each element X in left.equal_range(X), right.equal_range(X) is a permutation, using is_permutation.

operator!=

template<class Key, class Hash, class Pred, class Alloc>
    bool operator!=( [added with C++11]
        const unordered_set<Key, Hash, Pred, Alloc>& left,
        const unordered_set<Key, Hash, Pred, Alloc>& right);
template<class Key, class Hash, class Pred, class Alloc>
    bool operator!=( [added with C++11]
        const unordered_multiset<Key, Hash, Pred, Alloc>& left,
        const unordered_multiset<Key, Hash, Pred, Alloc>& right);

The template functions both return !(left == right).

unordered_multiset


allocator_type · begin · bucket · bucket_count · bucket_size · cbegin · cend · clear · const_iterator · const_local_iterator · const_pointer · const_reference · count · difference_type · emplace · emplace_hint · empty · end · equal_range · erase · find · get_allocator · hasher · hash_function · insert · iterator · key_eq · key_equal · key_type · load_factor · local_iterator · max_bucket_count · max_load_factor · max_size · operator= · pointer · reference · rehash · reserve · size · size_type · swap · unordered_multiset · value_type


template<class Key,
    class Hash = hash<Key>,
    class Pred = equal_to<Key>,
    class Alloc = allocator<Key> >
    class unordered_multiset {
public:
    typedef Key key_type;
    typedef Key value_type;
    typedef Hash hasher;
    typedef Pred key_equal;
    typedef Alloc allocator_type;

    typedef typename Alloc::pointer pointer;
    typedef typename Alloc::const_pointer const_pointer;
    typedef typename Alloc::reference reference;
    typedef typename Alloc::const_reference const_reference;
    typedef typename Alloc::size_type size_type;
    typedef typename Alloc::difference_type difference_type;

    typedef T0 iterator;
    typedef T1 const_iterator;
    typedef T2 local_iterator;
    typedef T3 const_local_iterator;

    explicit unordered_multiset(
        size_type nbuckets = N0,
        const Hash& hfn = Hash(),
        const Pred& comp = Pred(),
        const Alloc& al = Alloc());
    explicit unordered_multiset(const Alloc& al); [added with C++11]

    unordered_multiset(const unordered_multiset& right);
    unordered_multiset(const unordered_multiset& right, const Alloc& al); [added with C++11]
    unordered_multiset(unordered_multiset&& right); [added with C++11]
    unordered_multiset(unordered_multiset&& right, const Alloc& al); [added with C++11]

    template<class InIt>
        unordered_multiset(
        InIt first, InIt last,
        size_type nbuckets = N0,
        const Hash& hfn = Hash(),
        const Pred& comp = Pred(),
        const Alloc& al = Alloc());
    unordered_multiset(
        initializer_list<Ty> init,
        size_type nbuckets = N0,
        const Hash& hfn = Hash(),
        const Pred& comp = Pred(),
        const Alloc& al = Alloc()); [added with C++11]

    unordered_multiset& operator=(const unordered_multiset& right);
    unordered_multiset& operator=(initializer_list<Ty> init) [added with C++11]
    unordered_multiset& operator=(unordered_multiset&& right); [added with C++11]

    iterator begin() noexcept;
    const_iterator begin() const noexcept;
    local_iterator begin(size_type nbucket) noexcept;
    const_local_iterator begin(size_type nbucket) const noexcept;

    const_iterator cbegin() const noexcept; [added with C++11]
    const_local_iterator cbegin(size_type nbucket) const noexcept; [added with C++11]

    iterator end() noexcept;
    const_iterator end() const noexcept;
    local_iterator end(size_type nbucket) noexcept;
    const_local_iterator end(size_type nbucket) const noexcept;

    const_iterator cend() const noexcept; [added with C++11]
    const_local_iterator cend(size_type nbucket) const noexcept; [added with C++11]

    size_type size() const noexcept;
    size_type max_size() const noexcept;
    bool empty() const noexcept;

    size_type bucket_count() const noexcept;
    size_type max_bucket_count() const noexcept;
    size_type bucket(const Key& keyval) const;
    size_type bucket_size(size_type nbucket) const;

    Hash hash_function() const;
    Pred key_eq() const;
    Alloc get_allocator() const noexcept;

    float load_factor() const noexcept;
    float max_load_factor() const noexcept;
    void max_load_factor(float factor);
    void rehash(size_type nbuckets);
    void reserve(size_type nelements); [added with C++11]

    iterator insert(const value_type& val);
    iterator insert(const_iterator where, const value_type& val);
    template<class InIt>
        void insert(InIt first, InIt last);
    void insert(initializer_list<Ty> init) [added with C++11]
    iterator insert(value_type&& val); [added with C++11]
    iterator insert(const_iterator where, value_type&& val); [added with C++11]

    template<class... Valty>
        pair<iterator, bool> emplace(Valty&&... val); [added with C++11]
    template<class... Valty>
        iterator emplace_hint(const_iterator where, Valty&&... val); [added with C++11]

    iterator erase(const_iterator where);
    iterator erase(const_iterator first, const_iterator last);
    size_type erase(const Key& keyval);
    void clear() noexcept;

    void swap(unordered_multiset& right);

    const_iterator find(const Key& keyval) const;
    size_type count(const Key& keyval) const;
    pair<iterator, iterator>
        equal_range(const Key& keyval);
pair<const_iterator, const_iterator>
        equal_range(const Key& keyval) const;
    };

The template class describes an object that controls a varying-length sequence of elements of type const Key. The sequence is weakly ordered by a hash function, which partitions the sequence into an ordered set of subsequences called buckets. Within each bucket a comparison function determines whether any pair of elements has equivalent ordering. Each element serves as both a sort key and a value. The sequence is represented in a way that permits lookup, insertion, and removal of an arbitrary element with a number of operations that can be independent of the number of elements in the sequence (constant time), at least when all buckets are of roughly equal length. In the worst case, when all of the elements are in one bucket, the number of operations is proportional to the number of elements in the sequence (linear time). Moreover, inserting an element invalidates no iterators unless rehashing occurs, and removing an element invalidates only those iterators which point at the removed element.

The object orders the sequence it controls by calling two stored objects, a comparison function object of type key_equal and a hash function object of type hasher. You access the first stored object by calling the member function key_eq(); and you access the second stored object by calling the member function hash_function(). Specifically, for all values X and Y of type Key, the call key_eq()(X, Y) returns true only if the two argument values have equivalent ordering; the call hash_function()(keyval) yields a distribution of values of type size_t. Unlike template class unordered_set, an object of template class unordered_multiset does not ensure that key_eq()(X, Y) is always false for any two elements of the controlled sequence. (Keys need not be unique.)

The object also stores a maximum load factor, which specifies the maximum desired average number of elements per bucket. If inserting an element causes load_factor() to exceed the maximum load factor, the container increases the number of buckets and rebuilds the hash table as needed.

The actual order of elements in the controlled sequence depends on the hash function, the comparison function, the order of insertion, the maximum load factor, and the current number of buckets. You cannot in general predict the order of elements in the controlled sequence. You can always be assured, however, that any subset of elements that have equivalent ordering are adjacent in the controlled sequence.

The object allocates and frees storage for the sequence it controls through a stored allocator object of type allocator_type. Such an allocator object must have the same external interface as an object of template class allocator.

Inserting and erasing elements, and rehashing, preserves the order of elements with equivalent ordering.

unordered_multiset::allocator_type

typedef Alloc allocator_type;

The type is a synonym for the template parameter Alloc.

unordered_multiset::begin

iterator begin() noexcept;
const_iterator begin() const noexcept;
local_iterator begin(size_type nbucket) noexcept;
const_local_iterator begin(size_type nbucket) const noexcept;

The first two member functions return a forward iterator that points at the first element of the sequence (or just beyond the end of an empty sequence). The last two member functions return a forward iterator that points at the first element of bucket nbucket (or just beyond the end of an empty bucket).

unordered_multiset::bucket

size_type bucket(const Key& keyval) const;

The member function returns the bucket number currently corresponding to the key value keyval.

unordered_multiset::bucket_count

size_type bucket_count() const noexcept;

The member function returns the current number of buckets.

unordered_multiset::bucket_size

size_type bucket_size(size_type nbucket) const;

The member functions returns the size of bucket number nbucket.

unordered_multiset::cbegin

const_iterator cbegin() const noexcept; [added with C++11]
const_local_iterator cbegin(size_type nbucket) const noexcept; [added with C++11]

The first member function returns a forward iterator that points at the first element of the sequence (or just beyond the end of an empty sequence). The second member function returns a forward iterator that points at the first element of bucket nbucket (or just beyond the end of an empty bucket).

unordered_multiset::cend

const_reference cend() const noexcept; [added with C++11]
const_local_iterator cend(size_type nbucket) const noexcept; [added with C++11]

The first member function returns a forward iterator that points just beyond the end of the sequence. The second member function returns a forward iterator that points just beyond the end of bucket nbucket.

unordered_multiset::clear

void clear() noexcept;

The member function calls erase( begin(), end()).

unordered_multiset::const_iterator

typedef T1 const_iterator;

The type describes an object that can serve as a constant forward iterator for the controlled sequence. It is described here as a synonym for the implementation-defined type T1.

unordered_multiset::const_local_iterator

typedef T3 const_local_iterator;

The type describes an object that can serve as a constant forward iterator for a bucket. It is described here as a synonym for the implementation-defined type T3.

unordered_multiset::const_pointer

typedef typename Alloc::const_pointer const_pointer;

The type describes an object that can serve as a constant pointer to an element of the controlled sequence.

unordered_multiset::const_reference

typedef typename Alloc::const_reference const_reference;

The type describes an object that can serve as a constant reference to an element of the controlled sequence.

unordered_multiset::count

size_type count(const Key& keyval) const;

The member function returns the number of elements in the range delimited by equal_range(keyval).

unordered_multiset::difference_type

typedef typename Alloc::difference_type difference_type;

The signed integer type describes an object that can represent the difference between the addresses of any two elements in the controlled sequence.

unordered_multiset::emplace

template<class... Valty>
    pair<iterator, bool> emplace(Valty&&... val); [added with C++11]

The member function effectively returns insert(value_type(forward<Valty>(val)...)), except that the element value is constructed in place.

unordered_multiset::emplace_hint

template<class... Valty>
    iterator emplace_hint(const_iterator where, Valty&&... val); [added with C++11]

The member function effectively returns insert(where, value_type(forward<Valty>(val)...)), except that the element value is constructed in place.

unordered_multiset::empty

bool empty() const noexcept;

The member function returns true for an empty controlled sequence.

unordered_multiset::end

iterator end() noexcept;
const_iterator end() const noexcept;
local_iterator end(size_type nbucket) noexcept;
const_local_iterator end(size_type nbucket) const noexcept;

The first two member functions return a forward iterator that points just beyond the end of the sequence. The last two member functions return a forward iterator that points just beyond the end of bucket nbucket.

unordered_multiset::equal_range

pair<iterator, iterator>
    equal_range(const Key& keyval);
pair<const_iterator, const_iterator>
    equal_range(const Key& keyval) const;

The member function returns a pair of iterators X such that [X.first, X.second) delimits just those elements of the controlled sequence that have equivalent ordering with keyval. If no such elements exist, both iterators are end().

unordered_multiset::erase

iterator erase(const_iterator where);
iterator erase(const_iterator first, const_iterator last);
size_type erase(const Key& keyval);

The first member function removes the element of the controlled sequence pointed to by where. The second member function removes the elements in the range [first, last). Both return an iterator that designates the first element remaining beyond any elements removed, or end() if no such element exists.

The third member removes the elements in the range delimited by equal_range(keyval). It returns the number of elements it removes.

The member functions never throw an exception.

unordered_multiset::find

const_iterator find(const Key& keyval) const;

The member function returns equal_range(keyval).first.

unordered_multiset::get_allocator

Alloc get_allocator() const noexcept;

The member function returns the stored allocator object.

unordered_multiset::hash_function

Hash hash_function() const;

The member function returns the stored hash function object.

unordered_multiset::hasher

typedef Hash hasher;

The type is a synonym for the template parameter Hash.

unordered_multiset::insert

pair<iterator, bool> insert(const value_type& val);
iterator insert(const_iterator where, const value_type& val);
template<class InIt>
    void insert(InIt first, InIt last);
void insert(initializer_list<Ty> init) [added with C++11]
iterator insert(value_type&& val); [added with C++11]
iterator insert(const_iterator where, value_type&& val); [added with C++11]

The first member function inserts the element val in the controlled sequence, then returns the iterator that designates the inserted element.

The second member function returns insert(val), using where as a starting place within the controlled sequence to search for the insertion point. (Insertion can possibly occur somewhat faster, if the insertion point immediately precedes or follows where.)

The third member function inserts the sequence of element values, for each where in the range [first, last), by calling insert(*where).

The fourth member function inserts the sequence specified by an object of class initializer_list<Ty>.

The last two member functions behave the same as the first two but with an rvalue reference.

If an exception is thrown during the insertion of a single element, the container is left unaltered and the exception is rethrown. If an exception is thrown during the insertion of multiple elements, the container is left in a stable but unspecified state and the exception is rethrown.

unordered_multiset::iterator

typedef T0 iterator;

The type describes an object that can serve as a forward iterator for the controlled sequence. It is described here as a synonym for the implementation-defined type T0.

unordered_multiset::key_eq

Pred key_eq() const;

The member function returns the stored comparison function object.

unordered_multiset::key_equal

typedef Pred key_equal;

The type is a synonym for the template parameter Pred.

unordered_multiset::key_type

typedef Key key_type;

The type is a synonym for the template parameter Key.

unordered_multiset::load_factor

float load_factor() const noexcept;

The member function returns (float)size() / (float)bucket_count(), the average number of elements per bucket.

unordered_multiset::local_iterator

typedef T2 local_iterator;

The type describes an object that can serve as a forward iterator for a bucket. It is described here as a synonym for the implementation-defined type T2.

unordered_multiset::max_bucket_count

size_type max_bucket_count() const noexcept;

The member function returns the maximum number of buckets currently permitted.

unordered_multiset::max_load_factor

float max_load_factor() const noexcept;
void max_load_factor(float factor);

The first member function returns the stored maximum load factor. The second member function replaces the stored maximum load factor with factor.

unordered_multiset::max_size

size_type max_size() const noexcept;

The member function returns the length of the longest sequence that the object can control.

unordered_multiset::operator=

unordered_multiset& operator=(const unordered_multiset& right);
unordered_multiset& operator=(initializer_list<Ty> init) [added with C++11]
unordered_multiset& operator=(unordered_multiset&& right); [added with C++11]

The first member operator replaces the controlled sequence with a copy of the sequence controlled by right.

The second member operator replaces the controlled sequence from an object of class initializer_list<Ty>.

The third member operator is the same as the first, but with an rvalue reference.

unordered_multiset::pointer

typedef typename Alloc::pointer pointer;

The type describes an object that can serve as a pointer to an element of the controlled sequence.

unordered_multiset::reference

typedef typename Alloc::reference reference;

The type describes an object that can serve as a reference to an element of the controlled sequence.

unordered_multiset::rehash

void rehash(size_type nbuckets);

The member function alters the number of buckets to be at least nbuckets and rebuilds the hash table as needed.

unordered_multiset::reserve

void reserve(size_type nelements); [added with C++11]

The member function returns rehash(ceil(nelements / max_load_factor())).

unordered_multiset::size

size_type size() const noexcept;

The member function returns the length of the controlled sequence.

unordered_multiset::size_type

typedef T2 size_type;

The unsigned integer type describes an object that can represent the length of any controlled sequence. It is described here as a synonym for the implementation-defined type T2.

unordered_multiset::swap

void swap(unordered_multiset& right);

The member function swaps the controlled sequences between *this and right. If get_allocator() == right.get_allocator(), it does so in constant time, it throws an exception only as a result of copying the stored traits object of type Tr, and it invalidates no references, pointers, or iterators that designate elements in the two controlled sequences. Otherwise, it performs a number of element assignments and constructor calls proportional to the number of elements in the two controlled sequences.

unordered_multiset::unordered_multiset

explicit unordered_multiset(
    size_type nbuckets = N0,
    const Hash& hfn = Hash(),
    const Pred& comp = Pred(),
    const Alloc& al = Alloc());
explicit unordered_multiset(const Alloc& al);

unordered_multiset(const unordered_multiset& right);
unordered_multiset(const unordered_multiset& right, const Alloc& al); [added with C++11]
unordered_multiset(unordered_multiset&& right); [added with C++11]
unordered_multiset(unordered_multiset&& right, const Alloc& al); [added with C++11]

template<class InIt>
    unordered_multiset(
    InIt first, InIt last,
    size_type nbuckets = N0,
    const Hash& hfn = Hash(),
    const Pred& comp = Pred(),
    const Alloc& al = Alloc());
unordered_multiset(
    initializer_list<Ty> init,
    size_type nbuckets = N0,
    const Hash& hfn = Hash(),
    const Pred& comp = Pred(),
    const Alloc& al = Alloc()); [added with C++11]

All constructors initialize several stored values. For the copy constructor, the values are obtained from right. Otherwise:

The first two constructors specify an empty initial controlled sequence.

The next four constructors specify a copy of the sequence controlled by right. The last two of these constructors are the same as the first two, but with an rvalue reference.

The next constructor specifies the sequence of element values [first, last).

The last constructor specifies the initial controlled sequence with an object of class initializer_list<Ty>.

unordered_multiset::value_type

typedef Key value_type;

The type describes an element of the controlled sequence.

unordered_set


allocator_type · begin · bucket · bucket_count · bucket_size · cbegin · cend · clear · const_iterator · const_local_iterator · const_pointer · const_reference · count · difference_type · emplace · emplace_hint · empty · end · equal_range · erase · find · get_allocator · hasher · hash_function · insert · iterator · key_eq · key_equal · key_type · load_factor · local_iterator · max_bucket_count · max_load_factor · max_size · operator= · pointer · reference · rehash · reserve · size · size_type · swap · unordered_set · value_type


template<class Key,
    class Hash = hash<Key>,
    class Pred = equal_to<Key>,
    class Alloc = allocator<Key> >
    class unordered_set {
public:
    typedef Key key_type;
    typedef Key value_type;
    typedef Hash hasher;
    typedef Pred key_equal;
    typedef Alloc allocator_type;

    typedef typename Alloc::pointer pointer;
    typedef typename Alloc::const_pointer const_pointer;
    typedef typename Alloc::reference reference;
    typedef typename Alloc::const_reference const_reference;
    typedef typename Alloc::size_type size_type;
    typedef typename Alloc::difference_type difference_type;

    typedef T0 iterator;
    typedef T1 const_iterator;
    typedef T2 local_iterator;
    typedef T3 const_local_iterator;

    explicit unordered_set(
        size_type nbuckets = N0,
        const Hash& hfn = Hash(),
        const Pred& comp = Pred(),
        const Alloc& al = Alloc());
    explicit unordered_set(const Alloc& al); [added with C++11]

    unordered_set(const unordered_set& right);
    unordered_set(const unordered_set& right, const Alloc& al); [added with C++11]
    unordered_set(unordered_set&& right); [added with C++11]
    unordered_set(unordered_set&& right, const Alloc& al); [added with C++11]

    template<class InIt>
        unordered_set(
        InIt first, InIt last,
        size_type nbuckets = N0,
        const Hash& hfn = Hash(),
        const Pred& comp = Pred(),
        const Alloc& al = Alloc());
    unordered_set(
        initializer_list<Ty> init,
        size_type nbuckets = N0,
        const Hash& hfn = Hash(),
        const Pred& comp = Pred(),
        const Alloc& al = Alloc()); [added with C++11]

    unordered_set& operator=(const unordered_set& right);
    unordered_set& operator=(initializer_list<Ty> init) [added with C++11]
    unordered_set& operator=(unordered_set&& right); [added with C++11]

    iterator begin() noexcept;
    const_iterator begin() const noexcept;
    local_iterator begin(size_type nbucket) noexcept;
    const_local_iterator begin(size_type nbucket) const noexcept;

    const_iterator cbegin() const noexcept; [added with C++11]
    const_local_iterator cbegin(size_type nbucket) const noexcept; [added with C++11]

    iterator end() noexcept;
    const_iterator end() const noexcept;
    local_iterator end(size_type nbucket) noexcept;
    const_local_iterator end(size_type nbucket) const noexcept;

    const_iterator cend() const noexcept; [added with C++11]
    const_local_iterator cend(size_type nbucket) const noexcept; [added with C++11]

    size_type size() const noexcept;
    size_type max_size() const noexcept;
    bool empty() const noexcept;

    size_type bucket_count() const noexcept;
    size_type max_bucket_count() const noexcept;
    size_type bucket(const Key& keyval) const;
    size_type bucket_size(size_type nbucket) const;

    Hash hash_function() const;
    Pred key_eq() const;
    Alloc get_allocator() const noexcept;

    float load_factor() const noexcept;
    float max_load_factor() const noexcept;
    void max_load_factor(float factor);
    void rehash(size_type nbuckets);
    void reserve(size_type nelements); [added with C++11]

    pair<iterator, bool> insert(const value_type& val);
    iterator insert(const_iterator where, const value_type& val);
    template<class InIt>
        void insert(InIt first, InIt last);
    void insert(initializer_list<Ty> init) [added with C++11]
    pair<iterator, bool> insert(value_type&& val); [added with C++11]
    iterator insert(const_iterator where, value_type&& val); [added with C++11]

    template<class... Valty>
        pair<iterator, bool> emplace(Valty&&... val); [added with C++11]
    template<class... Valty>
        iterator emplace_hint(const_iterator where, Valty&&... val); [added with C++11]

    iterator erase(const_iterator where);
    iterator erase(const_iterator first, const_iterator last);
    size_type erase(const Key& keyval);
    void clear() noexcept;

    void swap(unordered_set& right);

    const_iterator find(const Key& keyval) const;
    size_type count(const Key& keyval) const;
    pair<iterator, iterator>
        equal_range(const Key& keyval);
    pair<const_iterator, const_iterator>
        equal_range(const Key& keyval) const;
    };

The template class describes an object that controls a varying-length sequence of elements of type const Key. The sequence is weakly ordered by a hash function, which partitions the sequence into an ordered set of subsequences called buckets. Within each bucket a comparison function determines whether any pair of elements has equivalent ordering. Each element serves as both a sort key and a value. The sequence is represented in a way that permits lookup, insertion, and removal of an arbitrary element with a number of operations that can be independent of the number of elements in the sequence (constant time), at least when all buckets are of roughly equal length. In the worst case, when all of the elements are in one bucket, the number of operations is proportional to the number of elements in the sequence (linear time). Moreover, inserting an element invalidates no iterators unless rehashing occurs, and removing an element invalidates only those iterators which point at the removed element.

The object orders the sequence it controls by calling two stored objects, a comparison function object of type key_equal and a hash function object of type hasher. You access the first stored object by calling the member function key_eq(); and you access the second stored object by calling the member function hash_function(). Specifically, for all values X and Y of type Key, the call key_eq()(X, Y) returns true only if the two argument values have equivalent ordering; the call hash_function()(keyval) yields a distribution of values of type size_t. Unlike template class unordered_multiset, an object of template class unordered_set ensures that key_eq()(X, Y) is always false for any two elements of the controlled sequence. (Keys are unique.)

The object also stores a maximum load factor, which specifies the maximum desired average number of elements per bucket. If inserting an element causes load_factor() to exceed the maximum load factor, the container increases the number of buckets and rebuilds the hash table as needed.

The actual order of elements in the controlled sequence depends on the hash function, the comparison function, the order of insertion, the maximum load factor, and the current number of buckets. You cannot in general predict the order of elements in the controlled sequence. You can always be assured, however, that any subset of elements that have equivalent ordering are adjacent in the controlled sequence.

The object allocates and frees storage for the sequence it controls through a stored allocator object of type allocator_type. Such an allocator object must have the same external interface as an object of template class allocator.

unordered_set::allocator_type

typedef Alloc allocator_type;

The type is a synonym for the template parameter Alloc.

unordered_set::begin

iterator begin() noexcept;
const_iterator begin() const noexcept;
local_iterator begin(size_type nbucket) noexcept;
const_local_iterator begin(size_type nbucket) const noexcept;

The first two member functions return a forward iterator that points at the first element of the sequence (or just beyond the end of an empty sequence). The last two member functions return a forward iterator that points at the first element of bucket nbucket (or just beyond the end of an empty bucket).

unordered_set::bucket

size_type bucket(const Key& keyval) const;

The member function returns the bucket number currently corresponding to the key value keyval.

unordered_set::bucket_count

size_type bucket_count() const noexcept;

The member function returns the current number of buckets.

unordered_set::bucket_size

size_type bucket_size(size_type nbucket) const;

The member functions returns the size of bucket number nbucket.

unordered_set::cbegin

const_iterator cbegin() const noexcept; [added with C++11]
const_local_iterator cbegin(size_type nbucket) const noexcept; [added with C++11]

The first member function returns a forward iterator that points at the first element of the sequence (or just beyond the end of an empty sequence). The second member function returns a forward iterator that points at the first element of bucket nbucket (or just beyond the end of an empty bucket).

unordered_set::cend

const_reference cend() const noexcept; [added with C++11]
const_local_iterator cend(size_type nbucket) const noexcept; [added with C++11]

The first member function returns a forward iterator that points just beyond the end of the sequence. The second member function returns a forward iterator that points just beyond the end of bucket nbucket.

unordered_set::clear

void clear() noexcept;

The member function calls erase( begin(), end()).

unordered_set::const_iterator

typedef T1 const_iterator;

The type describes an object that can serve as a constant forward iterator for the controlled sequence. It is described here as a synonym for the implementation-defined type T1.

unordered_set::const_local_iterator

typedef T3 const_local_iterator;

The type describes an object that can serve as a constant forward iterator for a bucket. It is described here as a synonym for the implementation-defined type T3.

unordered_set::const_pointer

typedef typename Alloc::const_pointer const_pointer;

The type describes an object that can serve as a constant pointer to an element of the controlled sequence.

unordered_set::const_reference

typedef typename Alloc::const_reference const_reference;

The type describes an object that can serve as a constant reference to an element of the controlled sequence.

unordered_set::count

size_type count(const Key& keyval) const;

The member function returns the number of elements in the range delimited by equal_range(keyval).

unordered_set::difference_type

typedef typename Alloc::difference_type difference_type;

The signed integer type describes an object that can represent the difference between the addresses of any two elements in the controlled sequence.

unordered_set::emplace

template<class... Valty>
    pair<iterator, bool> emplace(Valty&&... val); [added with C++11]

The member function effectively returns insert(value_type(forward<Valty>(val)...)), except that the element value is constructed in place.

unordered_set::emplace_hint

template<class... Valty>
    iterator emplace_hint(const_iterator where, Valty&&... val); [added with C++11]

The member function effectively returns insert(where, value_type(forward<Valty>(val)...)), except that the element value is constructed in place.

unordered_set::empty

bool empty() const noexcept;

The member function returns true for an empty controlled sequence.

unordered_set::end

iterator end() noexcept;
const_iterator end() const noexcept;
local_iterator end(size_type nbucket) noexcept;
const_local_iterator end(size_type nbucket) const noexcept;

The first two member functions return a forward iterator that points just beyond the end of the sequence. The last two member functions return a forward iterator that points just beyond the end of bucket nbucket.

unordered_set::equal_range

pair<iterator, iterator>
    equal_range(const Key& keyval);
pair<const_iterator, const_iterator>
    equal_range(const Key& keyval) const;

The member function returns a pair of iterators X such that [X.first, X.second) delimits just those elements of the controlled sequence that have equivalent ordering with keyval. If no such elements exist, both iterators are end().

unordered_set::erase

iterator erase(const_iterator where);
iterator erase(const_iterator first, const_iterator last);
size_type erase(const Key& keyval);

The first member function removes the element of the controlled sequence pointed to by where. The second member function removes the elements in the range [first, last). Both return an iterator that designates the first element remaining beyond any elements removed, or end() if no such element exists.

The third member removes the elements in the range delimited by equal_range(keyval). It returns the number of elements it removes.

The member functions never throw an exception.

unordered_set::find

const_iterator find(const Key& keyval) const;

The member function returns equal_range(keyval).first.

unordered_set::get_allocator

Alloc get_allocator() const noexcept;

The member function returns the stored allocator object.

unordered_set::hash_function

Hash hash_function() const;

The member function returns the stored hash function object.

unordered_set::hasher

typedef Hash hasher;

The type is a synonym for the template parameter Hash.

unordered_set::insert

pair<iterator, bool> insert(const value_type& val);
iterator insert(const_iterator where, const value_type& val);
template<class InIt>
    void insert(InIt first, InIt last);
void insert(initializer_list<Ty> init) [added with C++11]
pair<iterator, bool> insert(value_type&& val); [added with C++11]
iterator insert(const_iterator where, value_type&& val); [added with C++11]

The first member function determines whether an element X exists in the sequence whose key has equivalent ordering to that of val. If not, it creates such an element X and initializes it with val. The function then determines the iterator where that designates X. If an insertion occurred, the function returns pair(where, true). Otherwise, it returns pair(where, false).

The second member function returns insert(val).first, using where as a starting place within the controlled sequence to search for the insertion point. (Insertion can possibly occur somewhat faster, if the insertion point immediately precedes or follows where.)

The third member function inserts the sequence of element values, for each where in the range [first, last), by calling insert(*where).

The fourth member function inserts the sequence specified by an object of class initializer_list<Ty>.

The last two member functions behave the same as the first two but with an rvalue reference.

If an exception is thrown during the insertion of a single element, the container is left unaltered and the exception is rethrown. If an exception is thrown during the insertion of multiple elements, the container is left in a stable but unspecified state and the exception is rethrown.

unordered_set::iterator

typedef T0 iterator;

The type describes an object that can serve as a forward iterator for the controlled sequence. It is described here as a synonym for the implementation-defined type T0.

unordered_set::key_eq

Pred key_eq() const;

The member function returns the stored comparison function object.

unordered_set::key_equal

typedef Pred key_equal;

The type is a synonym for the template parameter Pred.

unordered_set::key_type

typedef Key key_type;

The type is a synonym for the template parameter Key.

unordered_set::load_factor

float load_factor() const noexcept;

The member function returns (float)size() / (float)bucket_count(), the average number of elements per bucket.

unordered_set::local_iterator

typedef T2 local_iterator;

The type describes an object that can serve as a forward iterator for a bucket. It is described here as a synonym for the implementation-defined type T2.

unordered_set::max_bucket_count

size_type max_bucket_count() const noexcept;

The member function returns the maximum number of buckets currently permitted.

unordered_set::max_load_factor

float max_load_factor() const noexcept;
void max_load_factor(float factor);

The first member function returns the stored maximum load factor. The second member function replaces the stored maximum load factor with factor.

unordered_set::max_size

size_type max_size() const noexcept;

The member function returns the length of the longest sequence that the object can control.

unordered_set::operator=

unordered_set& operator=(const unordered_set& right);
unordered_set& operator=(initializer_list<Ty> init) [added with C++11]
unordered_set& operator=(unordered_set&& right); [added with C++11]

The first member operator replaces the controlled sequence with a copy of the sequence controlled by right.

The second member operator replaces the controlled sequence from an object of class initializer_list<Ty>.

The third member operator is the same as the first, but with an rvalue reference.

unordered_set::pointer

typedef typename Alloc::pointer pointer;

The type describes an object that can serve as a pointer to an element of the controlled sequence.

unordered_set::reference

typedef typename Alloc::reference reference;

The type describes an object that can serve as a reference to an element of the controlled sequence.

unordered_set::rehash

void rehash(size_type nbuckets);

The member function alters the number of buckets to be at least nbuckets and rebuilds the hash table as needed.

unordered_set::reserve

void reserve(size_type nelements); [added with C++11]

The member function returns rehash(ceil(nelements / max_load_factor())).

unordered_set::size

size_type size() const noexcept;

The member function returns the length of the controlled sequence.

unordered_set::size_type

typedef typename Alloc::size_type size_type;

The unsigned integer type describes an object that can represent the length of any controlled sequence.

unordered_set::swap

void swap(unordered_set& right);

The member function swaps the controlled sequences between *this and right. If get_allocator() == right.get_allocator(), it does so in constant time, it throws an exception only as a result of copying the stored traits object of type Tr, and it invalidates no references, pointers, or iterators that designate elements in the two controlled sequences. Otherwise, it performs a number of element assignments and constructor calls proportional to the number of elements in the two controlled sequences.

unordered_set::unordered_set

explicit unordered_set(
    size_type nbuckets = N0,
    const Hash& hfn = Hash(),
    const Pred& comp = Pred(),
    const Alloc& al = Alloc());
explicit unordered_set(const Alloc& al);

unordered_set(const unordered_set& right);
unordered_set(const unordered_set& right, const Alloc& al); [added with C++11]
unordered_set(unordered_set&& right); [added with C++11]
unordered_set(unordered_set&& right, const Alloc& al); [added with C++11]

template<class InIt>
    unordered_set(
    InIt first, InIt last,
    size_type nbuckets = N0,
    const Hash& hfn = Hash(),
    const Pred& comp = Pred(),
    const Alloc& al = Alloc());
unordered_set(
    initializer_list<Ty> init,
    size_type nbuckets = N0,
    const Hash& hfn = Hash(),
    const Pred& comp = Pred(),
    const Alloc& al = Alloc()); [added with C++11]

All constructors initialize several stored values. For the copy constructor, the values are obtained from right. Otherwise:

The first two constructors specify an empty initial controlled sequence.

The next four constructors specify a copy of the sequence controlled by right. The last two of these constructors are the same as the first two, but with an rvalue reference.

The next constructor specifies the sequence of element values [first, last).

The last constructor specifies the initial controlled sequence with an object of class initializer_list<Ty>.

unordered_set::value_type

typedef Key value_type;

The type describes an element of the controlled sequence.

swap

template<class Key, class Hash, class Pred, class Alloc>
    void swap(
        unordered_multiset <Key, Hash, Pred, Alloc>& left,
        unordered_multiset <Key, Hash, Pred, Alloc>& right);
template<class Key, class Hash, class Pred, class Alloc>
    void swap(
        unordered_set <Key, Hash, Pred, Alloc>& left,
        unordered_set <Key, Hash, Pred, Alloc>& right);

The template function executes left.swap(right).


See also the Table of Contents and the Index.

Copyright © 1992-2013 by P.J. Plauger. All rights reserved.