2014-10-28 33 views
1

假設我有一個圖形,每個邊都包含一個字符。從一個頂點,我想要獲得特定字符的特定邊界。由於邊緣容器可以設置爲一個集合或一個哈希集合,我假設有一種方法可以做到這一點,而無需迭代頂點的外邊緣。我還假設/希望邊緣容器被鍵入邊緣包含的類型。在boost圖形庫中,我如何獲得頂點的特定外邊緣而不迭代該頂點的所有外邊緣?

#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! 

有沒有辦法做到這一點,或者是通過out-edges迭代唯一的選擇?

更新:

我認爲這將使我的容器。

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

這一切編譯和似乎工作,但它是巨大的醜陋。

回答

1

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

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

看到它Live On Coliru


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

#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 
+0

其實,我想要的是什麼:給定一個頂點,找到一個標「我」這個頂點散發出邊。所以邊緣e = get_specific_out_vertex(頂點,g,'i');.我的更新#3設法做到這一點,但以一種醜陋的方式。我已經將邊緣配置爲一個集合,所以希望對於每個頂點來說,只能有一個'i'邊緣。 – marathon 2014-10-29 16:12:26

相關問題