首页 > 解决方案 > C++ KOALA 图形库 - 理解语法以访问数据结构

问题描述

我正在使用C++ 图形库 KOALA来计算图形的最小切割。

这是我正在使用的示例 -example。它只是创建一个边缘容量的图并计算最小切割。

我的问题与这条线有关:

Flow::minEdgeCut(g, cap, s, t, Flow::outCut(blackHole, edgeIter()));

这是函数参数的文档

它说它返回最小切割通过的边缘。它会立即打印边缘,std::cout但我需要稍后在程序中访问它们。我的问题是如何访问它们存储的数据结构,例如在稍后阶段打印它们。

该示例将stuct edgeIter作为参数传递给outCutedgeIter提供 3 个重载运算符。我需要为这个结构添加一个额外的成员吗?

struct edgeIter {
    void operator=(MyGraph::PEdge e) { cout << e->info; }
    void operator++() { }
    edgeIter &operator*() { return *this; }
};

这里也是该outcut方法的定义。

/** \brief Auxiliary class to represent the edge cut. (output structure) */
    template< class VIter, class EIter > struct OutCut
    {
        VIter vertIter;/**<\brief Insert iterator  to the container with vertexes (accessible from starting vertex after the cut)*/
        EIter edgeIter;/**<\brief Insert iterator to the container with edges of the cat.*/
        /**\brief Constructor*/
        OutCut( VIter av, EIter ei ): vertIter( av ), edgeIter( ei ) { }
    };

/**\brief Generating function for the OutCut object.
 *
 *  \tparam VIter the type of insert iterator to container with vertices.
 *  \tparam EIter the type of insert iterator to container with edges.
 *  \param av the insert iterator to container with vertices.
 *  \tparam ei the insert iterator to container with edges.
 *
 *  [See example](examples/flow/example_Flow.html). */
template< class VIter, class EIter > static OutCut< VIter,EIter > outCut( VIter av, EIter ei )
    { return OutCut< VIter,EIter >( av,ei ); }

标签: c++templatesgraphoperator-overloading

解决方案


edgeIter是 的一个实例OutputIterator。您可以调整代码以使用 astd::back_inserter并将所有结果收集到向量edges中,如下所示:

std::vector<MyGraph::PEdge> edges;
Flow::minEdgeCut(g, cap, s, t, Flow::outCut(blackHole, std::back_inserter(edges)));

还有一个front_inserter,或者您可以编写一个自定义实现,例如edgeIter.


推荐阅读