使用BOOST复制边及其顶点和特性

copying edges with their vertices and properties using BOOST

本文关键字:顶点 BOOST 复制 使用      更新时间:2023-10-16

我想从dataG.front()中复制带有顶点和属性的边,并将其添加到testg中,我尝试了在"访问捆绑属性"部分中找到的内容http://www.boost.org/doc/libs/1_57_0/libs/graph/doc/bundles.html但它对我不起作用。PS:dataG是一个图的向量。

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部分),如何将顶点属性直接添加到添加的属性中,因为这里有重复。

附言:这里是完整的代码http://pastebin.com/2iztGAa6

正如您所注意到的,顶点可能是重复的,如果您将多个源图"合并"到一个图中,情况尤其如此。

如果你不介意重写顶点属性(并保留上次分配的值,以防值一直不相同),你可以使用属性映射:

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];

再说一遍,还有类似的等价访问:

testg[from] = gr[from];
testg[to]   = gr[to];

您甚至可以寻址单个捆绑成员:

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;

基于此文档页面的所有样本