没有成员作为唯一标识符的两个类实例的比较
Comparison of two class instances not having members as unique identifiers
考虑两个类,Node
和Edge
,分别代表多重图的节点和边缘(请参阅下面的MWE代码(。我的目的是使用三个unordered_map
:
(a( 从Node
变量到Edge
数据,
(b( 从Edge
变量到Node
数据,以及
(c( 从Node
对到double
变量。
我尝试为Node*
和Edge*
编写bool operator==()
函数,为Node*
、Edge*
和pair<Node*,Node*>
编写哈希函数。
我的第一个问题与Edge
的bool operator==()
功能有关。尽管 Node
s 的标签肯定是唯一的,但对于具有相同 start
s 和 end
s (( 的多个边,此bool operator==()
函数是不正确的。是否有机会使用例如Edge
的内存地址来构造正确的bool operator==()
函数?
第二个问题是,如果只假设简单的边,这些函数是否只导致保存不同的Node/Edge/pair<Node,Node>
对象。
所以,我的MWE如下:
#include<string>
#include<vector>
#include<utility>
#include<unordered_map>
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
Node(){};
string label;
bool operator==(const Node* other) const
{return label == other->label;};
};
class Edge
{
public:
Edge(){};
Node *start, *end;
double weight;
bool operator==(const Edge* other) const
{return start->label == other->start->label &&
end->label == other->end->label;};
//{return this == *other;}
};
namespace std
{
template <>
struct hash<Node*>
{
size_t operator()(const Node* node) const
{return hash<string>()(node->label);}
};
template <>
struct hash<Edge*>
{
size_t operator()(const Edge* edge) const
{
auto hash1 = hash<Node*>()(edge->start);
auto hash2 = hash<Node*>()(edge->end);
return hash1 ^ hash2;
}
};
template <>
struct hash<pair<Node*,Node*>>
{
size_t operator()(const pair<Node*, Node*>& p) const
{
auto hash1 = hash<Node*>()(p.first);
auto hash2 = hash<Node*>()(p.second);
return hash1 ^ hash2;
}
};
};
int main()
{
Edge* edge;
Node* node;
unordered_map<Node*,Edge> n2e;
unordered_map<Edge*,Node> e2n;
unordered_map<pair<Node*,Node*>,double> np2w;
edge = new Edge();
edge->weight = 1.0;
edge->start = new Node();
edge->start->label = "A";
edge->end = new Node();
edge->end->label = "B";
n2e[edge->start] = *edge;
e2n[edge] = *(edge->start);
np2w[make_pair(edge->start, edge->end)] = edge->weight;
edge = &n2e[edge->start];
node = &e2n[edge];
return 0;
}
你主要定义operator==(const Edge&, const Edge*)
,
而你需要operator==(const Edge*, const Edge*)
,但后者无法定义。
您必须编写定义operator()(const Edge*, const Edge*) const
并在std::unordered_map
中提供的类。
struct MyEdgeComp
{
bool operator()(const Edge* lhs, const Edge* rhs) const {
return *lhs == *rhs; // Assuming you implement it
}
};
std::unordered_map<Edge*, Node, std::hash<Edge*>, MyEdgeComp> e2n;
相关文章:
- 如何在C++中比较两个char数组
- 比较两个大小不等的映射c++
- 如何比较两个同名的预处理器宏?
- C++通过数字比较两个数字
- 关于比较两个无序数组并删除 a[] 中可以在 b[] 中找到的元素的问题
- 比较两个字符串后卡在无限循环中
- 比较两个节点坐标的最佳方法是什么?
- 比较C++中两个整数的最有效和最干净的方法是什么?
- 比较两个公钥与OpenSSL API
- 如何比较两个函数的速度和性能
- 如何在两个列表中比较和获取非包含值
- 绘制一个对象,比较模具缓冲区的两个不同值
- 比较两个 constexpr 指针不是 constexpr?
- 比较两个 std::chrono::time_point 实例时出错
- 比较C++中两个BST的节点
- C 中的两个比较运算符,例如Python
- 如何最好地重载运算符 < > <= >= 但只写一两个比较函数?
- 如何比较两个比较两个标准::设置
- 我可以使用memcmp两个比较多字节的字符串吗
- 这两个比较表达式相同吗?