修改 Boost::Graph 中的顶点属性
我试图弄清楚如何使用 boost::graph 来存储一些信息。 但是,我想将一些信息与每个顶点相关联。 盯着该库的文档会发现(a)文档写得很糟糕,或者(b),我显然没有我想象的那么擅长 C++。 选择两个。
我正在寻找一个简单的使用示例。
I am trying to figure out how to use boost::graph to store some information. However, there is information I want tied to each vertex. Staring at the documentation for the library reveals either(a)badly written documentation, or (b), I'm obviously not as good at C++ as I thought. Pick two.
I am looking for a simple example use.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
捆绑属性易于使用:
等等。
另请参阅文档。
另一种非常有用的顶点属性是外部属性。 您可以声明适当大小的 std::vectors 并将它们用作属性。
Bundled properties are straightforward to use:
and so on.
Please also refer to the docs.
The other type of vertex property that are very useful are external properties. You can declare
std::vectors
of the appropriate size and use them as properties.我不喜欢 boost::graph 的嵌套模板属性方法,因此我为所有内容编写了一个小包装器,基本上允许将任何结构/类作为顶点/边属性。 人们可以通过访问结构成员来访问属性。
为了保持灵活性,这些结构被定义为模板参数。
这里是代码:
使用它,您可以访问如下属性:
当然,您可能对图形结构有其他需求,但修改上面的代码应该非常容易。
I don't like the nested-template property approach of boost::graph, so I wrote a small wrapper around everything, that basically allows to put any struct/class as a vertex/edge property. One can access properties accessing the struct members.
To keep it flexible these structs are defined as template parameter.
Here the Code:
Using this you can access properties like this:
Of course you may have other needs for your graph's structure, but modification of the code above should be pretty easy.
下面是我用来将一些属性附加到顶点、边和图的代码。 请注意,顶点名称和图形名称是预定义的属性(完整列表请参阅 boost/properties.hpp),因此
vertex_name_t
和graph_name_t
已定义。 但是,vertex_location_t
、edge_length_t
和graph_notes_t
是我自己的属性,因此需要定义。 我从各种示例和文档中拼凑了这段代码,我不确定BOOST_INSTALL_PROPERTY
到底做了什么,但代码似乎工作正常。Below is code I used to attach some properties to vertices, edges, and graphs. Note that vertex name and graph name are predefined properties (see boost/properties.hpp for a complete list) so that
vertex_name_t
andgraph_name_t
are already defined. However,vertex_location_t
,edge_length_t
, andgraph_notes_t
are my own properties and hence need definition. I cobbled together this code from various examples and documentation, and I'm not sure exactly whatBOOST_INSTALL_PROPERTY
does, but the code seems to work fine.我认为 Boost.Graph 有一个非常好的文档,但并不真正适合初学者。 这是一个我希望足够简单的例子!
I consider Boost.Graph to have a very good documentation, but not truly for beginners on the matter. So here goes an example that i hope is simple enough !
我发现这些例子非常有用。 在 Windows 上,它将位于 \Program Files\boost\boost_1_38\libs\graph\example 目录中。
kevin_bacon2.cpp 使用顶点属性来存储参与者的名称。
您的顶点和边属性可以存储在常规结构或类中。
I found the examples pretty useful. On windows it will be in your \Program Files\boost\boost_1_38\libs\graph\example directory.
kevin_bacon2.cpp uses vertex properties to store the names of actors.
Your vertex and edge properties can be stored in regular structs or classes.