创建一个以自定义对象为键的unordered_map

时间:2019-03-23 04:21:36

标签: c++ hash unordered-map

我想创建一个以std::unordered_map为键的Node。我已经使运算符==<重载了。我还定义了一个哈希函数。但是,这给了我错误:

C2280: std::hash<_Kty>::hash(const std::hash<_Kty> &): attempting to reference a deleted function

这是我的Node类的代码:

#include "Node.h"

Node::Node() {
  word = Word();
}

Node::Node(Word input_word) : word(input_word) {}

Node::Node(const Node& other) {
  word = other.word;
  connection = other.connection;
}

Node::~Node() {
}

bool Node::operator<(const Node& other) {
  return (word < other.get_word());
}

bool Node::operator==(const Node& other) {
  if (word == other.word) {
    if (connection.size() == other.connection.size()) {
      for (unsigned int i = 0; i < connection.size(); i++) {
        if (connection[i] != other.connection[i]) {
          return false;
        }
      }
      return true;
    } else {
      return false;
    }
  } else {
    return false;
  }
}

Word Node::get_word() const {
  return word;
}

int Node::add_connection(Edge* input_node) {
  connection.push_back(input_node);
  return SUCCESS;
}

vector<Edge*> Node::retrieve_connection() const{
  return connection;
}


namespace std {
  template <> struct hash<Node> {
    size_t operator()(const Node& other) const {
      return hash<string>()(other.get_word().get_string()) ^
             hash<int>()(other.retrieve_connection().size());
    }
  };
}

我对接下来要添加的内容感到非常困惑。我尝试了我在网站上可以找到的大多数建议,但都没有用。

谢谢您的关注!

0 个答案:

没有答案