简体   繁体   English

图形专用模板类

[英]Specializing template class for graphs

Since there are a lot of graph algorithms that use residual graphs I thought it would make sense to implement a template class resGraph parametrized over the Objects Node and Edge which implements some basic functionality like reading a graph from a file, printing a flow and storing all the relevant information about the graph. 由于有很多图算法使用残差图,所以我认为在对象节点和边缘上实现参数化的模板类resGraph是有意义的,该模板实现了一些基本功能,例如从文件中读取图,打印流程并存储所有有关该图的相关信息。

Now I want to write the class PushRelabel which I want to be a specialization of the resGraph class template. 现在,我想编写类PushRelabel,它是resGraph类模板的特化对象。 But I don't just want to plug in some Node and Edge type, I also want to extend the functionality of the class, ie I want to add methods to the class. 但是我不仅要插入一些Node和Edge类型,还想扩展该类的功能,即我想向该类添加方法。 How can this be done? 如何才能做到这一点?

There are a lot of graph libraries, for example BGL from Boost. 有很多图形库,例如Boost的BGL Maybe you should look at them. 也许你应该看看他们。

Adding methods to template specialization is not an issue. 向模板专门化添加方法不是问题。 Note that specialization may have nothing to do with the original template. 请注意,专业化可能与原始模板无关。 For example: 例如:

template<typename T>
struct A
{
    void B(int);
};

template<>
struct A<int>
{
    float C(char*);
};

template<>
struct A<double>
{
    void D(int, int, int);
};

This is perfectly fine. 很好 For different types you will have instantiations that do not have anything in common to each other. 对于不同的类型,您将拥有彼此没有任何共同之处的实例化。 You can also write: 您还可以编写:

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);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM