【问题标题】:C++ KOALA graph library - understanding syntax to access a data structureC++ KOALA 图形库 - 理解访问数据结构的语法
【发布时间】:2019-03-19 09:39:31
【问题描述】:

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

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

我的问题与这条线有关:

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

这里是document for the arguments of the function

它表示它返回最小切割通过的边缘。它会立即使用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++ templates graph operator-overloading


    【解决方案1】:

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

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

    还有一个front_inserter,或者你可以写一个自定义实现,比如edgeIter

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多