使用unordered_map映射到一个类的实例到另一个类的实例

时间:2019-04-06 22:06:01

标签: c++ hash unordered-map

我有一个简单的代码,其中定义了四个Node型变量和两个Edge型变量,并创建了一个无序映射以将起始边缘映射到其末端边缘。但是,Visual Studio 2015专业版抱怨以下问题:

The C++ Standard doesn't provide a hash for this type.

能帮我解决这个问题吗?我的小代码(这是MWE,所以最初的目的不是引入这种愚蠢的地图),如下所示:

#include<string>
#include<vector>
#include<unordered_map>
#include<iostream>

using namespace std;

class Node
{
   public:
      Node(){};
      string label;
};

class Edge
{
   public:
      Edge(){};
      Node *start, *end;
};

int main()
{
   Edge* edge;
   Node* node;

   vector<Node*> nodes;
   vector<Edge*> edges;

   unordered_map<Node*,Edge*> n2e; 

   edge = new Edge();
   edges.push_back(edge);

   node = new Node();
   node->label = "A";
   edge->start = node;
   nodes.push_back(node);

   node = new Node();
   node->label = "B";
   edge->end = node;
   nodes.push_back(node);

   edge = new Edge();
   edges.push_back(edge);

   node = new Node();
   node->label = "C";
   edge->start = node;
   nodes.push_back(node);

   node = new Node();
   node->label = "D";
   edge->end = node;
   nodes.push_back(node);

   n2e[nodes[0]] = edges[0];
   n2e[nodes[2]] = edges[1];

   cout << n2e[nodes[0]]->start->label << "  " << n2e[nodes[0]]->end->label << endl;
   cout << n2e[nodes[2]]->start->label << "  " << n2e[nodes[2]]->end->label << endl;

   return 0;
}

0 个答案:

没有答案