使用一组结构,避免在一组结构中出现重复的结构



我试图表示一个非方向图。我创建了三个结构,如下所示。我在 Edge 结构中添加了运算符 == 和运算符 <,希望集合可以使用它来比较元素。

struct  Node;   /* Forward references to these two types */
struct Edge;     /* that the compiler can recognize them */
/* Type: Node
* This type represents an individual node and consists of the data of the 
*  node and the set of edges from this node */
struct Node{
int nodeNum;
string data;
set<Edge*> edges;
};

/* Type: Edge
* This type represents an individual edge and consists of pointers to the 
* endpoints */
struct Edge{
Node *end1;
Node *end2;
// This says that edge from node 1 to node 2 and edge from node 2 to node 1 are considered the same
bool operator==(const Edge &e) const{
return ( (this->end1->nodeNum == e.end1->nodeNum && this->end2->nodeNum == e.end2->nodeNum) ||
(this->end1->nodeNum == e.end2->nodeNum && this->end2->nodeNum == e.end1->nodeNum));
}
// This function is used by set to order elements of edges.
bool operator<(const Edge *e) const{
return (this->end1 < e->end1 && this->end2 < e->end2);
}
};

// This is a struct for graph
struct Graph{
set<Node*> Nodes;
set<Edge*> Edges;
map<int, Node*> nodeMap;
};

问题:如果说,我有一个从节点 1 到 2 的边和一个从 2 到 1 的边,我的结构声明说它们应该被认为是等效的。然而,当我将这两个边插入一个集合时,它会将它们作为两个单独的元素插入(即集合不理解边 1-2 和 2-1 相等(。我该怎么做才能让集合处理重复项(即只保留这些边缘之一(。例如,请参阅下文:

int main(){
// Let's make 2 nodes, node 1 and node 2
Node* n1 = new Node;
Node* n2 = new Node;
n1->nodeNum=1;
n2->nodeNum=2;
// Let's make 2 edges 1-2 and 2-1
Edge* e1 = new Edge;
Edge* e2 = new Edge;
e1->end1=n1; e1->end2=n2;
e2->end1=n2; e2->end2=n1;
// Now let's make a graph and put the edges in its internal set
Graph g;
g.Edges.insert(e1);  
g.Edges.insert(e2);  // the set takes in both e1 and e2. If I print all elements in g.Edges, it will print both 1-2 and 2-1
// How do I tell the set to treat e1 and e2 as equal edges so it took care of duplicates?
return 0;
}

std::set<T*>将创建一组内存位置,而不是一组 T 值。

如果要比较指向的物体,则需要提供自定义比较器:

struct Ptr_compare {
template<typename T>
constexpr bool operator()( const T* lhs, const T* rhs ) const {
return *lhs < *rhs;
}
};
// This is a struct for graph
struct Graph {
set<Node*, Ptr_compare> Nodes;
set<Edge*, Ptr_compare> Edges;
map<int, Node*> nodeMap;
};

然而:

请注意,我编写的代码回答了您的问题,但对于您的用例仍然不正确,只能将其用于非拥有指针,这绝对不是您的情况。

这不是我的解决方案本身的问题,而是您要完成的基本问题。某些东西需要在重复数据删除的对象上调用delete

最新更新