使用BOOST复制具有顶点和属性的边 [英] copying edges with their vertices and properties using BOOST
问题描述
我想从dataG.front()复制具有顶点和属性的边,并将其添加到testg,我尝试了在访问绑定属性部分 http://www.boost.org/doc/libs/1_57_0/libs/graph/doc/bundles.html 但它不为我工作。 PS:dataG是图形的向量。
I want to copy edges with their vertices and properties from dataG.front(), and add it to testg , I tried what I found in "Accessing bundled properties" section http://www.boost.org/doc/libs/1_57_0/libs/graph/doc/bundles.html but it isn't working for me. PS: dataG is a vector of graphs.
typedef std::pair<edge_iter, edge_iter> edge_pair;
Graph testg;
if (!dataG.empty())
{
auto const& gr = dataG.front();
for (edge_pair ep = edges(gr); ep.first != ep.second; ++ep.first) //ep edge number
{
auto ep = edges(gr).first; // ep edge number
vertex_t from = source(*ep.first, gr);
vertex_t to = target(*ep.first, gr);
boost::add_vertex(gr[from], testg);
boost::add_vertex(gr[to], testg);
boost::add_edge(from, to, gr[*ep.first], testg);
}
}
问题在源和目标。 (vertex_t和add_vertex部分),如何直接添加顶点属性到添加的一个,因为这里有一个重复。
edges properties works but there is a problem in source and target. (vertex_t and add_vertex part), How to add directly the vertices properties to the added one because there is a duplication here.
PS:更多的信息在这里是完整的代码 http://pastebin.com/2iztGAa6
PS: for more information here is the full code http://pastebin.com/2iztGAa6
推荐答案
您注意到,顶点可能会重复,如果您将多个源图形合并为一个图形,这一点尤其真实。
As you noticed, the vertices might be duplicated, and this is especially true if you "merge" multiple of the source graphs into one graph.
如果您不介意重写顶点属性(并保持最后分配的值,以防值不一致),您只需使用属性映射:
If you don't mind to re-write the vertex properties (and keeping the value last assigned in case the values aren't identical all the time) you can just use a property map:
boost::property_map<Graph, boost::vertex_bundle_t>::type vpmap = boost::get(boost::vertex_bundle, testg);
//so:
vpmap[from] = gr[from];
vpmap[to] = gr[to];
再次,还有类似的访问:
Then again, there's also the equivalent access like so:
testg[from] = gr[from];
testg[to] = gr[to];
您甚至可以添加单独的捆绑成员:
You can even addres individual bundled members:
boost::property_map<Graph, int VertexProperties::*>::type idmap = boost::get(&VertexProperties::id, testg);
boost::property_map<Graph, int VertexProperties::*>::type labelmap = boost::get(&VertexProperties::label, testg);
idmap[from] = gr[from].id;
labelmap[from] = gr[from].label;
基于此文档页
这篇关于使用BOOST复制具有顶点和属性的边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!