c++ - C++ KOALA 图形库 - 理解语法以访问数据结构
问题描述
我正在使用C++ 图形库 KOALA来计算图形的最小切割。
这是我正在使用的示例 -example。它只是创建一个边缘容量的图并计算最小切割。
我的问题与这条线有关:
Flow::minEdgeCut(g, cap, s, t, Flow::outCut(blackHole, edgeIter()));
这是函数参数的文档。
它说它返回最小切割通过的边缘。它会立即打印边缘,std::cout
但我需要稍后在程序中访问它们。我的问题是如何访问它们存储的数据结构,例如在稍后阶段打印它们。
该示例将stuct edgeIter
作为参数传递给outCut
。edgeIter
提供 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 ); }
解决方案
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
.
推荐阅读
- c++ - MSVC(Visual C++)是否有 -Weffc++ 等效项?
- java - 如何等到 HttpURLConnection 完成?
- xml - Python分别提取xml文件中的英文术语和法文术语(IATE数据库导出)
- apache-spark-sql - 如何为 ignite-spark-dataframe 配置缓存?
- php - 是否可以“反转” php str_shuffe?
- asp.net-mvc - Injecting a service into AccountController
- build - 在 MinGW64 中运行配置文件:未找到默认 build_alias 命令和默认前缀
- xml - 通过 PowerShell 保存 XML 总是插入行结尾不一致的注释
- append - 通过匹配的变量附加数据集
- python - 根据包含特定字符的字符串从列表中删除字符串