带有模板实现的C++有向图节点
C++ directed graph node with template implementation
我正在编写一个程序,该程序具有大量的有向图辅助函数,以便对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&参数。
- 构建可组合有向图(扫描仪生成器的汤普森构造算法)
- C++创建指向有向图中节点的指针数组
- 如何仅在 2 个节点之间获取最短路径,给定邻接列表有向图?
- 有向图 - 如何计算图中彼此顶点可到达的顶点数?
- C++计算有向图中的最短路径
- 使用非递归 DFS 检测有向图中的周期
- 如何将这些节点连接为有向图
- 如何使用广度优先搜索确定是否可以在有向图中到达顶点
- 在有向图中精确访问所有节点一次
- 在有向图中查找第二条最短路径
- 关于如何处理有向图的一个想法
- 找到有向图C 的最短路径
- 分析输入文件以创建有向图C++
- 在使用无序多映射实现的有向图中查找循环
- 如何找到在c++中实现的有向图的所有路径作为邻接列表
- 如何创建具有多个数据的节点的有向图
- 有向图中的广度优先搜索
- 查找路径的有向图邻接矩阵
- 从文件构建有向图
- 柠檬图库C++ - 有向图