| 类别:容器 | 组件类型:类型 |
Hash_set比较时,没有两个元素相等。在需要快速搜索元素的应用程序中很有用。但是,如果元素需要按特定顺序排列,则set更合适。
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");
}
| 参数 | 描述 | 默认值 |
|---|---|---|
| Key | hash_set 的键类型和值类型。这也定义为hash_set::key_type和hash_set::value_type | |
| HashFcn | hash_set 使用的哈希函数。这也定义为hash_set::hasher. | hash<Key> |
| EqualKey | hash_set 的键相等函数:一个二元谓词,用于确定两个键是否相等。这也定义为hash_set::key_equal. | equal_to<Key> |
| Alloc | thehash_set的分配器,用于所有内部内存管理。 | alloc |
| 成员 | 定义位置 | 描述 |
|---|---|---|
| value_type | 容器 | 对象类型,T,存储在 hash_set 中。 |
| key_type | 关联容器 | 与value_type. |
| hasher | 哈希关联容器 | thehash_set的哈希函数关联的键类型。 |
| key_equal | 哈希关联容器 | 比较键是否相等的函数对象。 |
| pointer | 容器 | 指向T. |
| reference | 容器 | 引用T |
| const_reference | 容器 | 常量引用T |
| size_type | 容器 | 无符号整型。 |
| difference_type | 容器 | 有符号整型。 |
| iterator | 容器 | 用于遍历hash_set. |
| const_iterator | 容器 | 用于遍历hash_set. (迭代器和const_iterator(两者是相同类型。) |
| iterator begin() const | 容器 | 返回一个iterator指向hash_set. |
| iterator end() const | 容器 | 返回一个iterator指向hash_set. |
| size_type size() const | 容器 | 返回hash_set. |
| size_type max_size() const | 容器 | 返回hash_set. |
| bool empty() const | 容器 | true如果hash_set的大小为0. |
| size_type bucket_count() const | 哈希关联容器 | 返回hash_set. |
| void resize(size_type n) | 哈希关联容器 | 将桶数增加到至少n. |
| hasher hash_funct() const | 哈希关联容器 | 返回hasher对象,由hash_set. |
| key_equal key_eq() const | 哈希关联容器 | 返回key_equal对象,由hash_set. |
| hash_set() | 容器 | 创建一个空的hash_set. |
| hash_set(size_type n) | 哈希关联容器 | 创建一个空的hash_set至少有n个桶。 |
hash_set(size_type n,
const hasher& h)
|
哈希关联容器 | 创建一个空的hash_set至少有n个桶,使用h作为哈希函数。 |
hash_set(size_type n,
const hasher& h,
const key_equal& k)
|
哈希关联容器 | 创建一个空的hash_set至少有n个桶,使用h作为哈希函数,并使用k作为键相等函数。 |
template <class InputIterator> hash_set(InputIterator f, InputIterator l) [1] |
唯一哈希关联容器 | 创建一个包含范围副本的 hash_set。 |
template <class InputIterator> hash_set(InputIterator f, InputIterator l, size_type n) [1] |
唯一哈希关联容器 | 创建一个包含范围副本的 hash_set,其桶数至少为n. |
template <class InputIterator> hash_set(InputIterator f, InputIterator l, size_type n, const hasher& h) [1] |
唯一哈希关联容器 | 创建一个包含范围副本的 hash_set,其桶数至少为n,使用h作为哈希函数。 |
hash_set(InputIterator f, InputIterator l,
size_type n, const hasher& h,
const key_equal& k)
[1]
|
唯一哈希关联容器 | 创建一个包含范围副本的 hash_set,其桶数至少为n,使用h作为哈希函数,并使用k作为键相等函数。 |
| hash_set(const hash_set&) | 容器 | 复制构造函数。 |
| hash_set& operator=(const hash_set&) | 容器 | 赋值运算符 |
| void swap(hash_set&) | 容器 | 交换两个 hash_set 的内容。 |
pair<iterator, bool> insert(const value_type& x) |
唯一关联容器 | 将x插入到hash_set. |
template <class InputIterator> void insert(InputIterator f, InputIterator l) [1] |
唯一关联容器 | 将一个范围插入到hash_set. |
| void erase(iterator pos) | 关联容器 | 删除pos. |
| size_type erase(const key_type& k) | 关联容器 | 删除键为k. |
| void erase(iterator first, iterator last) | 关联容器 | 删除一个范围内的所有元素。 |
| void clear() | 关联容器 | 删除所有元素。 |
| iterator find(const key_type& k) const | 关联容器 | 查找键为k. |
| size_type count(const key_type& k) const | 唯一关联容器 | 计算键为k. |
pair<iterator, iterator> equal_range(const key_type& k) const |
关联容器 | 查找一个包含所有键为k. |
bool operator==(const hash_set&,
const hash_set&)
|
哈希关联容器 | 测试两个 hash_set 是否相等。这是一个全局函数,而不是成员函数。 |
[1] 此成员函数依赖于成员模板函数,目前(1998 年初)并非所有编译器都支持。如果您的编译器支持成员模板,则可以使用任何类型的输入迭代器调用此函数。但是,如果您的编译器尚不支持成员模板,则参数必须是类型为const value_type*或类型为hash_set::const_iterator.