STL 映射自定义键类 [默认构造函数]

STL map Custom Key Class [default constructor]

本文关键字:默认 构造函数 映射 自定义 STL      更新时间:2023-10-16

>我有一个带有 Node 类的图形实现,如下所示

class Node {
public:
    Node() : idx(0), volume(1.0), futureVol(1.0), isCoarse(false) { }
    Node(index n) : idx(n), volume(1.0), futureVol(1.0), isCoarse(false) { }
    Node(const Node& a) : idx(a.idx), volume(a.volume), futureVol(a.futureVol), isCoarse(a.isCoarse) { }
    ...
    bool operator<(const Node& n) const {
        return futureVol > n.futureVol;
    }
    bool operator==(const Node& n) const {
        return idx > n.idx;
    }
    Node& operator=(const Node& node){
        if(this != &node){
            futureVol = node.futureVol;
            volume = node.volume;
            isCoarse = node.isCoarse;
            idx = node.idx;
        }
        return *this;
    }
private:
    index idx;
    double volume;
    double futureVol;
    bool isCoarse;
};

以及具有以下实现的图形:

class Graph{
private:
    std::map<Node,std::vector<Node>> edges;
    std::map<index, std::map<index, edgeweight>> edgeWeights;
    std::map<index, Node> nodes;
    Graph(const Graph& g);
    Graph& operator=(const Graph& g);
    count numNodes;
public:
    Graph(count& n); //default
    ~Graph(); //destructor
    Graph(std::vector<Node>&);
    void print();
    const std::vector<Node> neighbors(const index& idx) const;
    const std::vector<Node> coarseNeighbors(const index& idx) const;
    void addEdge(index &node1, index &node2, edgeweight& weight);
    void addEdges(std::map<index, std::map<index, edgeweight> >& e );
    edgeweight weight(const index& idx, const index& idx2) const;
    edgeweight weightedDegree(const index& idx) const;
    count degree(const index& idx) const;
    std::map<index, Node>& getNodes(){return nodes;}
    const count getSize() const { return numNodes; }
};
#endif

和图表.cpp:

#include "graph.h"
#include <algorithm>
Graph::Graph(count& n): 
edges(),
edgeWeights(),
nodes(),
numNodes(n)
{
    for(index i=0; i<numNodes;i++){
        Node n(i);
        nodes[i] = n;   
    }
}
...
void Graph::addEdge(index& n1, index& n2, edgeweight& weight){
    edgeWeights[n1][n2]= weight;
    edgeWeights[n2][n1]= weight;
    edges[nodes[n1]].push_back(nodes[n2]);
    edges[nodes[n2]].push_back(nodes[n1]);
}
...

问题是每当我添加新边缘时。调用 Node 的默认构造函数,我最终将 0 作为节点 Id,而不是传递给addEdge的原始节点,例如addEdge(1,2,4.0)会将边缘 0 <--> 2 添加到图形中。任何帮助将不胜感激。

我尝试编写如下自定义哈希函数,但没有帮助:

namespace std
{
    template <>
    struct hash<Node>
    {
        size_t operator()(const Node& n) const
        {
            return (hash<float>()(n._index()) >> 1);
        }
    };
}

回想一下,std::map使用operator<,从不operator==。它认为两个键等效,只要!(key1 < key2) && !(key2 < key1) .

您的Node::operator<比较futureVol,您从未将其设置为默认值 1.0 以外的任何值,因此就std::map<Node, ...>而言,您的程序创建的所有节点都彼此等效。因此,edges地图只有一个条目。当你写的时候

edges[nodes[n1]].push_back(nodes[n2]);
edges[nodes[n2]].push_back(nodes[n1]);

这两个语句更新同一条目。