簡體   English   中英

在boost graph lib中,如何在不遍歷該頂點的所有邊緣的情況下獲得該頂點的特定邊緣?

[英]in boost graph lib, how do I get a specific out-edge of a vertex without iterating over all the out-edges of that vertex?

假設我有一個圖,每個圖的邊都包含一個字符。 從一個頂點,我想獲得具有特定字符的特定邊緣。 由於可以將邊緣容器設置為一個集或一個哈希集,因此我認為有一種方法可以做到這一點而無需遍歷頂點的邊緣。 我還假設/希望邊緣容器鍵入邊緣包含的類型。

#include <boost/graph/adjacency_list.hpp>

using namespace boost;
typedef boost::adjacency_list<setS, vecS, directedS, std::string, char> MyGraph;
typedef boost::graph_traits<MyGraph>::vertex_descriptor Vertex;
typedef boost::graph_traits<MyGraph>::edge_descriptor Edge;

MyGraph g;

//setup
add_vertex(std::string("xxx"), g);
Vertex currentVertex = g.vertex_set()[0];
Vertex endVertex = add_vertex(std::string("yyy"), g);
add_edge(currentVertex, endVertex, 'i', g);

//later...
//Now I want that edge containing the letter 'i'.

//out_edges returns a pair of edge iterators.
std::pair<iterator, iterator> iterators = out_edges(currentVertex, g);  // do not want!

Edge iEdge = how_do_I_get_This?(currentVertex, g); // want!

有沒有辦法做到這一點,還是在邊緣進行迭代是唯一的選擇?

更新:

我認為這將為我提供容器。

std::set<?> edges = g.out_edge_list(currentVertex);

現在我不知道是什么? 模板類型為。

UPDATE2:

這似乎可以編譯,但是我需要一個edge_descriptor,而不是edge_property才能傳遞給目標。

 std::set<boost::detail::stored_edge_property<long unsigned int, char> > edges = fGraph.out_edge_list(currentVertex);

UPDATE3:

猜猜我不需要邊緣描述符。 得到了我需要的東西:

 std::set<boost::detail::stored_edge_property<long unsigned int, char> > edges = fGraph.out_edge_list(currentVertex);
 std::_Rb_tree_const_iterator<boost::detail::stored_edge_property<long unsigned int, char> >  edge = edges.find(*i);

 Vertex target = edge.get_target();

所有這些都可以編譯並且似乎可以工作,但是它非常難看。

您是否正在尋找如何使用邊緣描述符?

Edge i_edge = add_edge(currentVertex, endVertex, 'i', g).first;

i_edge'i'邊緣的頂點描述符。

// later...
// Now I want that edge containing the letter 'i'.
char yougotit = g[i_edge];

核實:

assert('i' == yougotit);

在Coliru上實時觀看


如果你真的想進行搜索,並且可以使用C ++ 1Y你可能會發現這個優雅: 還住

#include <boost/graph/adjacency_list.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/adaptors.hpp>
#include <iostream>

using namespace boost::adaptors;

using namespace boost;
typedef boost::adjacency_list<setS, vecS, directedS, std::string, char> MyGraph;
typedef boost::graph_traits<MyGraph>::vertex_descriptor Vertex;
typedef boost::graph_traits<MyGraph>::edge_descriptor Edge;

int main() {
    MyGraph g;

    // setup
    add_vertex(std::string("xxx"), g);
    Vertex currentVertex = g.vertex_set()[0];
    Vertex endVertex = add_vertex(std::string("yyy"), g);
    add_edge(currentVertex, endVertex, 'i', g);

    for (auto matching : boost::edges(g) | filtered([&g](auto const& e) { return g[e] == 'i'; }))
        std::cout << matching << " --> " << g[matching] << "\n";
}

輸出:

(0,1) --> i

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM