5

我以为我可以有一个指向完全专业化的模板函数的指针,但是下面的代码没有编译(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 = 5;
    unordered_map<string, string> mymap(n, (const std::hash<string> &)hashing_func, (const std::equal_to<string> &)(key_equal_fn<string>)) ;

    mymap["paul"] = "jenna";
    mymap["frank"] = "ashley";


    return 0;
}

构造函数行返回以下错误:

错误 C2440: 'type cast' : 无法从 'bool (__cdecl *)(T,T)' 转换为 'const std::equal_to<_Ty> &'

4

1 回答 1

12

hashing_func和都key_equal_fn应该是函子对象(而不是函数)。另外,它们的类型必须提供给unordered_map模板,即地图应该有这个类型:

unordered_map<string, string, hashing_func, key_equal_fn>

wherehashing_funckey_equal_fn是函子类:

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

struct key_equal_fn {
    bool operator()(const string& t1, const string& t2) const {
        return !(t1.compare(t2));
    }
};

那么,mymap是这样定义的:

typedef unordered_map<string, string, hashing_func, key_equal_fn> MapType;
MapType::size_type n = 5;
MapType mymap(n, hashing_func(), key_equal_fn());

或者,hashing_func和/或key_equal_fn可以是函数,但您必须将它们包装到std::function对象中。那是,

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

bool key_equal_fn(const string& t1, const string& t2){
  return !(t1.compare(t2));
}

mymap以这种方式定义

typedef unordered_map<string, string,
    std::function<unsigned long(const string&)>,
    std::function<bool(const string&, const string&)>> MapType;

MapType::size_type n = 5;
MapType mymap(n, hashing_func, key_equal_fn);

如果您愿意,可以使用 lambdas 并避免编写两个函数或仿函数类:

typedef unordered_map<string, string,
    std::function<unsigned long(const string&)>,
    std::function<bool(const string&, const string&)>> MapType;

MapType mymap(n,
  [](const string& key) -> unsigned long {
      unsigned long hash = 0;
      for(size_t i=0; i<key.size(); i++)
        hash += (71*hash + key[i]) % 5;
      return hash;
  },
  [](const string& t1, const string& t2) {
       return !(t1.compare(t2));
  });

最后,我最喜欢的是全 lambdas解决方案

auto hashing_func = [](const string& key) -> unsigned long {
    unsigned long hash = 0;
    for(size_t i=0; i<key.size(); i++)
        hash += (71*hash + key[i]) % 5;
    return hash;
};

auto key_equal_fn = [](const string& t1, const string& t2) {
    return !(t1.compare(t2));
};

typedef unordered_map<string, string,
    decltype(hashing_func), decltype(key_equal_fn)> MapType;

MapType::size_type n = 5;
MapType mymap(n, hashing_func, key_equal_fn);
于 2013-04-04T11:14:29.043 回答