2010-06-23 28 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

相关问题