unordered_map定义如下:
template<class Key,
class Ty,
class Hash = std::hash<Key>,
class Pred = std::equal_to<Key>,
class Alloc = std::allocator<std::pair<const Key, Ty> > >
class unordered_map;
> class unordered_map
第1个参数,存储key值。
第2个参数,存储mapped value。
第3个参数,为哈希函数的函数对象。它将key作为参数,并利用函数对象中的哈希函数返回类型为size_t的唯一哈希值。默认值为std::hash< key >。
第4个参数,为等比函数的函数对象。它内部通过等比操作符’=='来判断两个key是否相等,返回值为bool类型。默认值是std::equal_to< key >。
如果想用哈希的时候,但是哈希的目标又不再STL标准的类型内,比如一个自定义的class,就不太方便使用STL默认的哈希函数,比较函数,那么就需要重写了。
将自定义类型作为unordered_map的键值,需如下两个步骤:
1.定义自定义key的哈希函数的函数对象,告知此容器如何生成hash的值;
2.定义等比函数的函数对象或者在自定义类里重载operator==(), 告知容器当出现hash冲突的时候,如何区分hash值相同的不同对象。
示例代码如下:
#include <iostream>
#include <unordered_set>
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
class Line{
public:
int k;
int b;
Line(int d1, int d2){
k = d1;
b = d2;
}
bool operator==(const Line& other)const{//重载operator==(),若没有重载==则定义 unordered_map 时需要isEqual
return other.k ==k && other.b == b;
}
};
struct createhash {
size_t operator()( const Line l) const // size_t
{
//return size_t(l.k ^ l.b);//自定义哈希
return hash<int>()(l.k) ^ hash<int>()(l.b);
}
};
struct isEqual {
bool operator()( const Line l1, const Line l2) const//最后的const不能少
{
return l1.k == l2.k && l1.b == l2.b;
}
};
int main(){
//unordered_map<Line ,int , createhash, isEqual> mm;//若使用这种方式,Line类中不需要重载==
unordered_map<Line ,int , createhash> mm;
mm.insert({Line(1,2),1});
mm.insert({Line(2,3),2});
auto success = mm.insert({Line(2,3),2});
if(success.second == false)
std::cout<<"mm insert failed "<<std::endl;
for(auto ele : mm)
{
std::cout<< ele.first.k<<" " <<ele.first.b<<std::endl;
}
//unordered_set<Line, createhash> ms;
unordered_set<Line, createhash, isEqual> ms;//若使用这种方式,Line类中不需要重载==
ms.insert(Line(2,3));
auto it = ms.insert(Line(2,3));
if(it.second == false)
std::cout<<"ms insert failed "<<std::endl;
for(auto ele : ms)
{
std::cout<< ele.k<<" " <<ele.b<<std::endl;
}
return 0;
}
c++ unordered_set,unordered_map中自定义哈希函数_unordered_map 自定义hash函数_Let'sCode的博客-CSDN博客
public class Student {
int grade;
int cls;
String firstName;
String lastName;
Student(int grade, int cls, String firstName, String lastName){
this.grade = grade;
this.cls = cls;
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public int hashCode(){
int B = 31;
int hash = 0;
hash = hash * B + ((Integer)grade).hashCode();
hash = hash * B + ((Integer)cls).hashCode();
hash = hash * B + firstName.toLowerCase().hashCode();
hash = hash * B + lastName.toLowerCase().hashCode();
return hash;
}
@Override
public boolean equals(Object o){
if(this == o)
return true;
if(o == null)
return false;
if(getClass() != o.getClass())
return false;
Student another = (Student)o;
return this.grade == another.grade &&
this.cls == another.cls &&
this.firstName.toLowerCase().equals(another.firstName.toLowerCase()) &&
this.lastName.toLowerCase().equals(another.lastName.toLowerCase());
}
}
import java.util.Map;
import java.util.TreeMap;
public class HashTable<K, V> {
private static final int upperTol = 10;
private static final int lowerTol = 2;
private static final int initCapacity = 7;
private TreeMap<K, V>[] hashtable;
private int size;
private int M;
public HashTable(int M){
this.M = M;
size = 0;
hashtable = new TreeMap[M];
for(int i = 0 ; i < M ; i ++)
hashtable[i] = new TreeMap<>();
}
public HashTable(){
this(initCapacity);
}
private int hash(K key){
return (key.hashCode() & 0x7fffffff) % M;
}
public int getSize(){
return size;
}
public void add(K key, V value){
TreeMap<K, V> map = hashtable[hash(key)];
if(map.containsKey(key))
map.put(key, value);
else{
map.put(key, value);
size ++;
if(size >= upperTol * M)
resize(2 * M);
}
}
public V remove(K key){
V ret = null;
TreeMap<K, V> map = hashtable[hash(key)];
if(map.containsKey(key)){
ret = map.remove(key);
size --;
if(size < lowerTol * M && M / 2 >= initCapacity)
resize(M / 2);
}
return ret;
}
public void set(K key, V value){
TreeMap<K, V> map = hashtable[hash(key)];
if(!map.containsKey(key))
throw new IllegalArgumentException(key + " doesn't exist!");
map.put(key, value);
}
public boolean contains(K key){
return hashtable[hash(key)].containsKey(key);
}
public V get(K key){
return hashtable[hash(key)].get(key);
}
private void resize(int newM){
TreeMap<K, V>[] newHashTable = new TreeMap[newM];
for(int i = 0 ; i < newM ; i ++)
newHashTable[i] = new TreeMap<>();
int oldM = M;
this.M = newM;
for(int i = 0 ; i < oldM ; i ++){
TreeMap<K, V> map = hashtable[i];
for(K key: map.keySet())
newHashTable[hash(key)].put(key, map.get(key));
}
this.hashtable = newHashTable;
}
}