hash_set哈希集合容器是一种使用哈希表(hashtable)数据结构的具有高效数据检索的关联容器。作为一种集合容器,它的元素不论有多少个分量 ,都视为一个单一的数据类型,元素的键值就是元素本身。根据元素键值的大小,按照某个函数式子(哈希函数)直接计算元素的哈希地址,确定元素的具体存放位置。哈希函数是一个多对一的函数,不同元素可能具有相同的哈希地址。进行检索时,先计算元素的哈希地址,然后就可用较少的键值比较,将元素检索出来。
一、hash_set技术原理
二、hash_set应用基础
#include <hash_set>
1、创建hash_set对象
hash_set()
hash_set(size_type n)
hash_set(size_type n, const hasher &h)
hash_set(size_type n, const hasher &h, const key_equal &k)
hash_set(const hash_set &)
2、元素的插入
pair<iterator, bool> insert(const value_type &v)
void insert(InputIterator first, InputIterator last)
3、元素的删除
void erase(iterator position)
size_type erase(const key_type &k)
void erase(iterator first, iterator last)
void clear()
4、元素的遍历访问
iterator begin()
iterator end()
5、元素的搜索
iterator find(const key_type &k) const
6、其他的常用函数
bool empty() const
size_type size() const
size_type bucket_count() const
#include <iostream>
#include <hash_set>
int main(void) {
using namespace std;
hash_set<int> hs;
hs.insert(10);
hs.insert(20);
hs.insert(80);
hs.insert(90);
hs.insert(100);
//
cout << hs.empty() << endl;
cout << hs.size() << endl;
cout << hs.bucket_count() << endl;
return 0;
}