问题
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);
}
}
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: for more information here is the full code http://pastebin.com/2iztGAa6
回答1:
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;
All the samples based on this documentation page
来源:https://stackoverflow.com/questions/28809958/copying-edges-with-their-vertices-and-properties-using-boost