一. 定义
"哈希集合 " 是一种基于哈希表实现的无序集合,用于存储互不相同的元素,支持高效的插入、删除和查找操作(平均 O(1) 时间复杂度)。
哈希表的键实际上 就是 "哈希集合"
哈希集合中,哈希标的元素不处理,用bool值来代替(或者说来判断)----占位符
二.代码:
cpp
#include<iostream>
#include<unordered_map>
using namespace std;
template<typename K>
class MyhashSet{
private:
unordered_map<K,bool> map;
public:
void add(const K& key){
map[key] = true;
}
void remove(const K& key){
map.erase(key);
}
bool contains(const K& key){
return map.find(key) != map.end();
}
int size()const{
return map.size();
}
};
本质上: 操纵哈希集合,就是操纵哈希表的键。
三. stl 哈希集合
unordered_set<数据类型> 变量名