c++ - 图形专用模板类

标签 c++ class templates graph

由于有很多使用残差图的图形算法,我认为在对象节点和边上实现一个参数化的模板类 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/

相关文章:

c++ - 没有辅助类型的依赖类型

c++ - 如何对 std::vector 进行排序但不使用 std::sort 更改特定元素?

c++ - google-test 和 static constexpr 成员

c++ - QTableWidget:只允许数字

c++ - 矩阵类实验中的编译错误 - "expected initializer before ‘.’ token ”

c++ - undefined reference (模板)

c++ - 如果注册表项不存在

C++11 在类中放置枚举类的重载运算符

java - 它显示客户已定义的错误。请让我知道发生了什么错误以及如何纠正它

C++ - 试图重载 "<<"运算符