由于有很多使用残差图的图形算法,我认为在对象节点和边上实现一个参数化的模板类 resGraph 是有意义的,它实现了一些基本功能,比如从文件中读取图形、打印流并存储有关图形的所有相关信息。
现在我想编写类 PushRelabel,我想将其作为 resGraph 类模板的特化。但我不只是想插入一些 Node 和 Edge 类型,我还想扩展类的功能,即我想给类添加方法。如何做到这一点?
最佳答案
有很 multimap 库,比如BGL从升压。也许你应该看看它们。
向模板特化添加方法不是问题。请注意,特化可能与原始模板无关。例如:
template<typename T>
struct A
{
void B(int);
};
template<>
struct A<int>
{
float C(char*);
};
template<>
struct A<double>
{
void D(int, int, int);
};
这很好。对于不同的类型,您将拥有彼此没有任何共同点的实例化。你也可以这样写:
template<typename T>
struct B : public A<T> // Class template can have base class.
{ // It can be either a class or instantiation
void B(int); // of some class template.
void B2(int, int, int);
};
template<>
struct B<int> : public A<int> // Specialization for int.
{
void B(int);
void B2(int, int, int);
};
关于c++ - 图形专用模板类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41321241/