簡體   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