仅将 std::vector 属性的元素传递给 BGL 算法
我有一个图,其中多个边权重存储为
namespace boost {
enum edge_weightvector_t {
edge_weightvector = 1337
};
BOOST_INSTALL_PROPERTY(edge, weightvector);
}
typedef boost::adjacency_list<
boost::vecS,
boost::vecS,
boost::undirectedS,
boost::no_property,
boost::property<boost::edge_weightvector_t, std::vector<int> >
> graph_t;
权重全部推到向量上。
现在我想调用 prim_minimum_spanning_tree()< /code>
函数在图上,向量中的第一个元素用作权重。
如何执行正确的函数调用?
I have a graph with multiple edge weightings stored as
namespace boost {
enum edge_weightvector_t {
edge_weightvector = 1337
};
BOOST_INSTALL_PROPERTY(edge, weightvector);
}
typedef boost::adjacency_list<
boost::vecS,
boost::vecS,
boost::undirectedS,
boost::no_property,
boost::property<boost::edge_weightvector_t, std::vector<int> >
> graph_t;
The weightings are all pushed onto the vector.
Now I want to call the prim_minimum_spanning_tree()
function on the graph, with the first elements in the vector used as weightings.
How can I perform a correct function call?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我现在已经做到了,首先将所需的权重复制到附加属性,然后运行算法并随后复制回来。它很丑陋,但对我来说却很有效。
I've did it now by first copying the desired weightings to an additional property, then running the algorithm and copying back afterwards. It is ugly, but it does the trick in my case.
我最近尝试做同样的事情(使用向量属性),但未能仅使用其中一个值运行算法。但是,我发现使用 外部属性 是这是一种不会导致不必要的复制操作并将属性映射显式传递给算法的好方法。
如果您使用随机访问容器,则可以使用
boost::iterator_property_map
来包装该容器并使其成为property_map
。它不需要边缘描述符,而是需要基于 0 的边缘索引来实现边缘和属性值之间的有效映射。这是要点,进一步完成后,您会找到完整的示例:这里是一个完整的示例:
I recently tried to do the same (to use a vector property) and failed to run algorithms only with one of the values. However, I found that using exterior properties is a good approach that won't lead to unnecessary copy actions and to pass the property map explicitly to the algorithm.
If you use random access containers you can use
boost::iterator_property_map
that will wrap that container and make it aproperty_map
. Instead of edge descriptors it requires 0-based edge indices for the efficient mapping between edges and property values. Here is the punchline, further done you find the complete example:And here a complete example: