繁体   English   中英

使用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是图的向量。

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

    }
}

edge属性有效,但源和目标存在问题。 (vertex_t和add_vertex部分),如何直接将顶点属性添加到添加的属性,因为这里有重复。

PS:有关更多信息,请参阅完整代码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;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM