带有模板实现的C++有向图节点

C++ directed graph node with template implementation

本文关键字:C++ 有向图 节点 实现      更新时间:2023-10-16

我正在编写一个程序,该程序具有大量的有向图辅助函数,以便对C++有更深入的理解。其中一个中心对象被称为节点,它具有成员函数,可以帮助计算节点之间的行程。我试图更好地理解在面向对象设计中使用C++模板。

以下是Node类的快速快照

class Node {
    friend void swap(Node & first, Node & second) {
        using std::swap;
        swap(first.name, second.name);  
    }
public:
    Node(std::string val);
    Node(const Node & copy);
    Node & operator = (Node copy) {
        swap(*this, copy);
        return *this;
    }
    bool operator < (Node & rhs) const {
        return (size < rhs.size);
    }
    bool operator > (Node & rhs) const {
        return (size > rhs.size);
    }
    bool insertEdge(Node * dest, int distToNode);
    // I'd like for this return type to not be tied to an int
    // Especially if weights were represented as floats or doubles
    int findTravelDistance(Node * const & toNode) const;
    int findTravelDistance(std::queue<Node *> * const & nodeRoute) const;
    // Mutators
    void setNodeName(const std::string nameToSet);
    std::string getNodeName() const;
    void setNodeSize(const int size);
    int getNodeSize() const;
    // Misc
    void toString() const;
    // Constants
    static const bool ALLOW_CIRCULAR;
    ~Node();
protected:

private:
    int size;
    std::string name;
    // Here int represents the weight of the edge. I would like it to be able to be
    // declared as an int, float, long, or double etc...
    std::map<Node *, int> * travelEdges;
}; // end class
} // end namespace

当我构建这个类以包含更多的功能时,我发现自己在为如何使我的功能更具适应性而挣扎。例如,查看findTravelDistance函数。

我想做的是让表示权重的返回类型与类型无关,而有序映射数据结构的值与类型无关。正如目前实现的那样,用户只能为权重声明一个类型int。我意识到我可能会开始函数过载。但是,我觉得这太多余了,明显违反了DRY原则。如果我必须改变这个函数的工作方式,我就必须为每一次过载都改变它。所以我的直觉告诉我应该使用C++模板。由于我是模板的新手,我很难在哪里声明它。如果我让find函数成为模板函数,并只返回泛型类型。。

template<class T>
T findTravelDistance(std::queue<Node *> * const & nodeRoute) const;

这将解决我在那里的问题。但是,它并不能解决表示边的底层地图数据结构只能包含int的问题。我的下一个想法是声明一个类模板。。

template<class T>
class Node { ... }

但这对我来说也很奇怪。这意味着声明和初始化看起来像

Node<float> * n = new Node<float>("N");

如果我是我的程序的用户,我不会立即将Node与表示边权重的float类型相关联。

那么,在这种情况下,模板的最佳或适当用法是什么呢?或者在这里使用模板甚至是正确的路径?我的课程设计可能一开始就有缺陷,而且不太符合C++的要求。如有任何反馈,我们将不胜感激。

这是一个非常干净的代码:)。欢迎使用C++!

我相信你想做的是使用一个模板变量来保持你的边权重。下面这样的东西怎么样:

using std::swap;
template<class Distance>
class Node {
friend void swap(Node & first, Node & second) {  
    swap(first.name, second.name);  
}
public:
Node(std::string val);
Node(const Node & copy);
Node & operator = (Node copy) {
    swap(*this, copy);
    return *this;
}
bool operator < (Node & rhs) const {
    return (size < rhs.size);
}
bool operator > (Node & rhs) const {
    return (size > rhs.size);
}
bool insertEdge(Node * dest, Distance distToNode);
// I'd like for this return type to not be tied to an int
// Especially if weights were represented as floats or doubles
Distance findTravelDistance(Node * const & toNode) const;
Distance findTravelDistance(std::queue<Node *> * const & nodeRoute) const;
// Mutators
void setNodeName(const std::string nameToSet);
std::string getNodeName() const;
void setNodeSize(const Distance size);
int getNodeSize() const;
// Misc
void toString() const;
// Constants
static const bool ALLOW_CIRCULAR;
~Node();
private:
 int size;
std::string name;
std::map<Node *, Distance> * travelEdges;
}; // end class

作为奖励,我已经将您的using声明移到了类的顶部。通常这些都在文件的顶部。你也可以看看Parashift C++常见问题解答这本圣经,特别是关于常量正确性的部分。例如,您的comparator方法应该具有constNode&参数。