将自定义顶点添加到增强图 [英] adding custom vertices to a boost graph

查看:25
本文介绍了将自定义顶点添加到增强图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我用类 CElement 定义了 n 个元素,如何使用增强图创建这些元素的顶点 - 并将它们连接起来?我见过捆绑了 boost 图的道具,但我就是想不通这个.

If I have n elements defined with class CElement, how can one create vertices of those elements with boost graph - and connect them also? I've seen boost graph bundled props, but I just can't figure this one out.

推荐答案

我不明白你到底想做什么.您想将一些数据与顶点相关联吗?然后使用捆绑属性.

I don't understand what you want to do exactly. Do you want to associate some data to vertices? Then use bundled properties.

//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";

还有其他设置属性的方法,但您有一个引导示例.

The are other ways to set the properties, but there you a have an example to bootstrap.

希望我没有误解这个问题.

I hope I didn't misunderstand the question.

这篇关于将自定义顶点添加到增强图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆