# hash_set<Key, HashFcn, EqualKey, Alloc>

Category: containers | Component type: type

### Description

`Hash_set` is a Hashed Associative Container that stores objects of type `Key`. `Hash_set` is a Simple Associative Container, meaning that its value type, as well as its key type, is `Key`. It is also a Unique Associative Container, meaning that no two elements compare equal using the Binary Predicate `EqualKey`.

`Hash_set` is useful in applications where it is important to be able to search for an element quickly. If it is important for the elements to be in a particular order, however, then `set` is more appropriate.

### Example

    struct eqstr
    {
      bool operator()(const char* s1, const char* s2) const
      {
        return strcmp(s1, s2) == 0;
      }
    };

    void lookup(const hash_set<const char*, hash<const char*>, eqstr>& Set,
                const char* word)
    {
      hash_set<const char*, hash<const char*>, eqstr>::const_iterator it
        = Set.find(word);
      cout << word << ": "
           << (it != Set.end() ? "present" : "not present")
           << endl;
    }

    int main()
    {
      hash_set<const char*, hash<const char*>, eqstr> Set;
      Set.insert("kiwi");
      Set.insert("plum");
      Set.insert("apple");
      Set.insert("mango");
      Set.insert("apricot");
      Set.insert("banana");

      lookup(Set, "mango");
      lookup(Set, "apple");
      lookup(Set, "durian");
    }

### Definition

Defined in the header hash_set, and in the backward-compatibility header hash_set.h. This class is an SGI extension; it is not part of the C++ standard.

### Template parameters

Parameter | Description | Default
--- | --- | ---
`Key` | The hash_set's key type and value type. This is also defined as `hash_set::key_type` and `hash_set::value_type` |
`HashFcn` | The Hash Function used by the hash_set. This is also defined as `hash_set::hasher`. | `hash<Key>`
`EqualKey` | The hash_set's key equality function: a binary predicate that determines whether two keys are equal. This is also defined as `hash_set::key_equal`. | `equal_to<Key>`
`Alloc` | The `hash_set`'s allocator, used for all internal memory management. | `alloc`

### Model of

Unique Hashed Associative Container, Simple Associative Container

### Type requirements

- `Key` is Assignable.
- `EqualKey` is a Binary Predicate whose argument type is `Key`.
- `EqualKey` is an equivalence relation.
- `Alloc` is an Allocator.

### Public base classes

None.

### Members

Member | Where defined | Description
--- | --- | ---
`value_type` | Container | The type of object, `T`, stored in the hash_set.
`key_type` | Associative Container | The key type associated with `value_type`.
`hasher` | Hashed Associative Container | The `hash_set`'s Hash Function.
`key_equal` | Hashed Associative Container | Function object that compares keys for equality.
`pointer` | Container | Pointer to `T`.
`reference` | Container | Reference to `T`
`const_reference` | Container | Const reference to `T`
`size_type` | Container | An unsigned integral type.
`difference_type` | Container | A signed integral type.
`iterator` | Container | Iterator used to iterate through a `hash_set`.
`const_iterator` | Container | Const iterator used to iterate through a `hash_set`. (`Iterator` and `const_iterator` are the same type.)
`iterator begin() const` | Container | Returns an `iterator` pointing to the beginning of the `hash_set`.
`iterator end() const` | Container | Returns an `iterator` pointing to the end of the `hash_set`.
`size_type size() const` | Container | Returns the size of the `hash_set`.
`size_type max_size() const` | Container | Returns the largest possible size of the `hash_set`.
`bool empty() const` | Container | `true` if the `hash_set`'s size is `0`.
`size_type bucket_count() const` | Hashed Associative Container | Returns the number of buckets used by the `hash_set`.
`void resize(size_type n)` | Hashed Associative Container | Increases the bucket count to at least `n`.
`hasher hash_funct() const` | Hashed Associative Container | Returns the `hasher` object used by the `hash_set`.
`key_equal key_eq() const` | Hashed Associative Container | Returns the `key_equal` object used by the `hash_set`.
`hash_set()` | Container | Creates an empty `hash_set`.
`hash_set(size_type n)` | Hashed Associative Container | Creates an empty `hash_set` with at least `n` buckets.
    hash_set(size_type n,
             const hasher& h)
| Hashed Associative Container | Creates an empty `hash_set` with at least `n` buckets, using `h` as the hash function.
    hash_set(size_type n,
             const hasher& h,
             const key_equal& k)
| Hashed Associative Container | Creates an empty `hash_set` with at least `n` buckets, using `h` as the hash function and `k` as the key equal function.
    template <class InputIterator>
    hash_set(InputIterator f, InputIterator l)
    [1]
| Unique Hashed Associative Container | Creates a hash_set with a copy of a range.
    template <class InputIterator>
    hash_set(InputIterator f, InputIterator l,
             size_type n)
    [1]
| Unique Hashed Associative Container | Creates a hash_set with a copy of a range and a bucket count of at least `n`.
    template <class InputIterator>
    hash_set(InputIterator f, InputIterator l,
             size_type n, const hasher& h)
    [1]
| Unique Hashed Associative Container | Creates a hash_set with a copy of a range and a bucket count of at least `n`, using `h` as the hash function.
    hash_set(InputIterator f, InputIterator l,
             size_type n, const hasher& h,
             const key_equal& k)
    [1]
| Unique Hashed Associative Container | Creates a hash_set with a copy of a range and a bucket count of at least `n`, using `h` as the hash function and `k` as the key equal function.
`hash_set(const hash_set&)` | Container | The copy constructor.
`hash_set& operator=(const hash_set&)` | Container | The assignment operator
`void swap(hash_set&)` | Container | Swaps the contents of two hash_sets.
    pair<iterator, bool>
    insert(const value_type& x)
| Unique Associative Container | Inserts `x` into the `hash_set`.
    template <class InputIterator>
    void insert(InputIterator f, InputIterator l)
    [1]
| Unique Associative Container | Inserts a range into the `hash_set`.
`void erase(iterator pos)` | Associative Container | Erases the element pointed to by `pos`.
`size_type erase(const key_type& k)` | Associative Container | Erases the element whose key is `k`.
`void erase(iterator first, iterator last)` | Associative Container | Erases all elements in a range.
`void clear()` | Associative Container | Erases all of the elements.
`iterator find(const key_type& k) const` | Associative Container | Finds an element whose key is `k`.
`size_type count(const key_type& k) const` | Unique Associative Container | Counts the number of elements whose key is `k`.
    pair<iterator, iterator>
    equal_range(const key_type& k) const
| Associative Container | Finds a range containing all elements whose key is `k`.
    bool operator==(const hash_set&,
                    const hash_set&)
| Hashed Associative Container | Tests two hash_sets for equality. This is a global function, not a member function.

### New members

All of `hash_set`'s members are defined in the Unique Hashed Associative Container and Simple Associative Container requirements. `Hash_set` does not introduce any new members.

### Notes

[1] This member function relies on member template functions, which at present (early 1998) are not supported by all compilers. If your compiler supports member templates, you can call this function with any type of input iterator. If your compiler does not yet support member templates, though, then the arguments must either be of type `const value_type*` or of type `hash_set::const_iterator`.

### See also

Associative Container, Hashed Associative Container, Simple Associative Container, Unique Hashed Associative Container, `set`, `map`, `multiset`, `multimap`, `hash_map`, `hash_multiset`, `hash_multimap`

Copyright © 1999 Silicon Graphics, Inc. All Rights Reserved. TrademarkInformation