2010-06-23 32 views
16

如果我有用CElement類定義的n個元素,如何用boost圖創建這些元素的頂點並將它們連接起來? 我看過boost圖形捆綁道具,但我無法弄清楚這一點。將自定義頂點添加到提升圖

+0

抱歉不清楚。 CElements的實例是頂點。我希望能夠添加,刪除,連接和斷開CElements的這些實例。我真的需要定義具有pt到CElement實例的struct Vertex,還是有更優雅的方法? – dodol 2010-06-23 09:04:44

回答

50

我不明白你想要做什麼。你想把一些數據關聯到頂點嗎?然後使用捆綁的屬性。

//Define a class that has the data you want to associate to every vertex and edge 
struct Vertex{ int foo;} 
struct Edge{std::string blah;} 

//Define the graph using those classes 
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::directedS, Vertex, Edge > Graph; 
//Some typedefs for simplicity 
typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t; 
typedef boost::graph_traits<Graph>::edge_descriptor edge_t; 

//Instanciate a graph 
Graph g; 

// Create two vertices in that graph 
vertex_t u = boost::add_vertex(g); 
vertex_t v = boost::add_vertex(g); 

// Create an edge conecting those two vertices 
edge_t e; bool b; 
boost::tie(e,b) = boost::add_edge(u,v,g); 


// Set the properties of a vertex and the edge 
g[u].foo = 42; 
g[e].blah = "Hello world"; 

其他的方法來設置屬性,但你有一個引導示例。

我希望我沒有誤解這個問題。

+1

我認爲,而不是edge_t e = boost :: add_edge(u,v,g); 應該寫 edge_t e; bool加入; boost :: tie(e,added)= boost :: add_edge(u,v,g); – dodol 2010-06-23 11:28:58

+0

謝謝!我糾正 – 2010-06-23 16:17:06

+2

@Tristram「比使用捆綁屬性更容易」 - 你在這個答案中描述的正是* IS *捆綁的屬性。 =) – wjl 2013-07-19 14:35:11

相關問題