我试图代表一个无方向图。我创建了三个结构,如下所示。我将运算符==和运算符

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有一条边,我的struct声明说它们应该被认为是等效的。但是,当我将这两个边缘插入到集合中时,它将两个元素都插入为两个单独的元素(即集合不了解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

关于c++ - 使用一组结构,并避免一组重复的结构,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52747379/

10-12 21:26