![](/img/trans.png)
[英]BGL: Using bundled properties to store vertex descriptor of another vertex
[英]BGL: Unable to access bundled vertex properties
我有一個這樣定義的虛擬類:
class Graph
{
friend std::ostream& operator<<(std::ostream&, const ArchGraph &);
struct VertexProperty;
struct EdgeProperty;
typedef boost::vecS vertex_selector;
typedef boost::vecS edge_selector;
typedef boost::property<boost::vertex_name_t, VertexProperty> vertex_property;
typedef boost::property<boost::edge_name_t, EdgeProperty> edge_property;
typedef boost::adjacency_list<
vertex_selector, edge_selector, boost::bidirectionalS,
vertex_property, edge_property> adjacency_type;
typedef size_t size_type;
struct VertexProperty {
size_type id;
};
struct EdgeProperty {
adjacency_type::edges_size_type index;
size_type id;
};
public:
void foo() const;
private:
adjacency_type _adj;
};
在foo
方法中,我嘗試遍歷鄰接列表_adj
中的所有頂點,並打印每個頂點屬性的id
成員:
void Graph::foo() const
{
for (auto v : boost::make_iterator_range(boost::vertices(_adj))) {
std::cout << _adj[v].id;
}
}
這不會編譯,顯然_adj[v]
類型為const struct boost::no_property
,這不是我期望的。
對於我來說,這似乎有些荒謬,因為似乎有很多例子都采用這種方法。
我正在使用Boost 1.67.0,有人可以啟發我這里做錯了什么嗎? 該文檔在這方面不是很有幫助。
使用property<tag, type>
不是捆綁的屬性¹。
您正在談論的所有這些示例都將使用此樣式:
typedef boost::adjacency_list<
edge_selector, vertex_selector, boost::bidirectionalS,
VertexProperty, EdgeProperty> adjacency_type;
請注意,您將
edge_selector
和vertex_selector
顛倒了。
當然,現在您不能向前聲明。 因此,在定義adjacency_list本身之前,您需要找到另一種訪問圖特征的方法:
typedef boost::adjacency_list_traits<edge_selector, vertex_selector, boost::bidirectionalS> traits;
因此,您可以提前設置尺寸類型:
typedef traits::vertices_size_type size_type;
typedef traits::edges_size_type edges_size_type;
#include <boost/graph/adjacency_list.hpp>
#include <iostream>
struct ArchGraph;
class Graph
{
friend std::ostream& operator<<(std::ostream&, const ArchGraph &);
struct VertexProperty;
struct EdgeProperty;
typedef boost::vecS vertex_selector;
typedef boost::vecS edge_selector;
typedef boost::adjacency_list_traits<edge_selector, vertex_selector, boost::bidirectionalS> traits;
typedef traits::vertices_size_type size_type;
typedef traits::edges_size_type edges_size_type;
struct VertexProperty {
size_type id;
};
struct EdgeProperty {
edges_size_type index;
size_type id;
};
typedef boost::adjacency_list<
edge_selector, vertex_selector, boost::bidirectionalS,
VertexProperty, EdgeProperty> adjacency_type;
public:
Graph() : _adj(3) {
for (auto vd : boost::make_iterator_range(vertices(_adj)))
_adj[vd].id = (vd+1)*10;
}
void foo() const;
private:
adjacency_type _adj;
};
void Graph::foo() const
{
for (auto v : boost::make_iterator_range(boost::vertices(_adj))) {
std::cout << _adj[v].id << " ";
}
}
int main() {
Graph g;
g.foo();
}
打印
10 20 30
¹(相反,如果我沒記錯的話,這就是舊式的所謂“內部屬性”)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.