相关疑难解决方法(0)

unordered_map哈希函数c ++

我需要像这样定义一个unordered_map unordered_map<pair<int, int>, *Foo>,定义和传递一个hashequal函数到这个地图的语法是什么?

我试过传递给它这个对象:

class pairHash{
public:
    long operator()(const pair<int, int> &k) const{
        return k.first * 100 + k.second;
    }
};
Run Code Online (Sandbox Code Playgroud)

没有运气:

unordered_map<pair<int, int>, int> map = unordered_map<pair<int, int>, int>(1,
*(new pairHash()));
Run Code Online (Sandbox Code Playgroud)

我不知道是什么size_type_Buskets意思所以我给了它1.做正确的方法是什么?谢谢.

c++ unordered-map hash-function

21
推荐指数
3
解决办法
2万
查看次数

unordered_map构造函数错误(equal_to模板化函数)

我以为我可以指向一个完全专用的模板函数,但下面的代码没有编译(MSVC2012)

#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm>

using namespace std;

unsigned long hashing_func(string key)
{
    unsigned long hash = 0;
    for(int i=0; i<key.size(); i++)
    {
        hash += (71*hash + key[i]) % 5;
    }
    return hash;
}

bool key_equal_fn2(string t1, string t2)
{
    return t1 == t2;
}


template<class T> bool key_equal_fn(T t1, T t2)
{
    return t1 == t2;
}

template <> bool key_equal_fn<string>(string t1, string t2)
{
    return !(t1.compare(t2));
}

int main ()
{
    unordered_map<string, string>::size_type n …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

5
推荐指数
1
解决办法
3194
查看次数

标签 统计

c++ ×2

c++11 ×1

hash-function ×1

unordered-map ×1