【问题标题】:Hashing a template type散列模板类型
【发布时间】:2017-06-07 09:19:50
【问题描述】:

在这里,我尝试制作一个地图,它的顶点可以是用户定义的类。但是当我尝试将模板类型元素添加到 unordered_set 时,它会出错。代码是:

#include <iostream>
#include <unordered_set>
#include <string>
#include <vector>
#include <functional>

template<class T> class Edge;

template<class T> class Vertex{          // Made it a class just for its constructor.
    public:
        template<class A> Vertex(A vert){
            A vertex = vert;
            std::unordered_set<Edge<A>> adjlist; 
        }
};

template<class T> class Edge{       // Made it a class just for its constructor.
    public:
        template<class A> Edge(Vertex<A> vert1, Vertex<A> vert2, int w){    
            Vertex<A> *origin = &vert1;
            Vertex<A> *target = &vert2;
        }
    };


template<class T> 
class WUG{
    private:
        std::unordered_set<Vertex<T>> vertices;
        std::unordered_set<Edge<T>> edges;
        int num_of_edges;
        int num_of_vertices;
    public:
        WUG() {
            num_of_edges = 0;
            num_of_vertices = 0;
        } 
        void addVertex(T newVert) {
            Vertex<T> temp = Vertex<T>(newVert);             
            vertices.emplace(temp);                 //Problem is here
        }
int main(int argc, char** argv) {
    WUG<char> g1 = WUG<char>();
    g1.addVertex('A');
    g1.addVertex('B');
    g1.addVertex('C');

    return 0;
}

错误:它打开 hashtable_policy.h 并在

处给出错误
    template <typename _Key, typename _Hash>
         struct __is_noexcept_hash : std::integral_constant<bool,
         noexcept(declval<const _Hash&>()(declval<const _Key&>()))> //Here
    { };
    [Error] no match for call to '(const std::hash<Vertex<char> >) (const Vertex<char>&)'

如何将 template 类型的对象置入 unordered_set?一对 2 模板怎么样?

【问题讨论】:

    标签: c++ templates hash graph compiler-errors


    【解决方案1】:

    我相信您需要提供特殊的散列和比较函数才能使散列集(或在我的示例中为散列映射)工作。这是一个最小的例子。使用 C++11 测试。

    #include <unordered_map>
    #include <iostream>
    #include <algorithm>
    
    template<typename T>
    struct foo {
      typedef T value_type;
      foo(T x) : x(x) {}
      T x;
    };
    
    template<typename T>
    struct foo_hasher {
      int operator()(const T &val) const {
        return std::hash<typename T::value_type>()(val.x);
      }
    };
    
    template<typename T>
    struct foo_equality {
      bool operator()(const T &left, const T& right) const {
        return left.x == right.x;
      }
    };
    
    int main() {
      typedef std::unordered_map<foo<int>, int, foo_hasher<foo<int>>, foo_equality<foo<int>>> Map;
      Map mp;
      foo<int> x(5);
      mp[x] = 10;
      mp[foo<int>(10)] = 22;
    
      std::for_each(mp.begin(), mp.end(), [](const Map::value_type &val) {
        std::cout << val.first.x << ", " << val.second << "\n";
      });
    }
    

    请注意,我的散列函数和相等函数都不是限制性的——它们是 wrt T 而不是 foo,但主体应该是相同的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-31
      • 2018-09-13
      • 1970-01-01
      • 2011-07-26
      • 2019-03-22
      • 2015-07-05
      • 2017-07-11
      • 1970-01-01
      相关资源
      最近更新 更多