[英]How to modify properties while proceeding BFS in Boost Graph Library?
我正在為圖表使用捆綁屬性。 定義如下:
class Node
{
void AssignPlane(Plane& p)
Plane* dp;
double errors;
}
void Node::AssignPlane(Plane& p)
{
dp=&p;
errors=p.a+p.b+p.c;// simplified
}
typedef adjacency_list<vecS,vecS,bidirectionalS,Node,float> NGraph;
//...
struct NVisitor: default_bfs_visitor
{
void discover_vertex(VertexDesc u, const NGraph& g) const
{
// CAN'T MODIFY G
}
}
但是我不能簡單地調用g [u] .AssignPlane(p)來修改頂點,也不能獲得指向頂點的指針,這對我來說都是至關重要的。
盡管對於Boost的新手來說,這個問題似乎很愚蠢,並且已經花了兩周時間來適應復雜的Boost代碼風格,但我確實需要幫助。
請不要嘗試回答“您需要使用BGL以外的其他東西”,因為除了BGL之外我什么都找不到支持我的工作的東西。
我還必須說,官方文檔並不是要用一種簡單的方式來解釋他們的出色工作。 由於我已經閱讀了數十次文檔,因此建議不要重新閱讀該文檔。
感謝您的幫助,在此先感謝您。
您可以使字段可變
class Node
{
void AssignPlane(Plane& p) const;
Plane* mutable dp;
double mutable errors;
}
void Node::AssignPlane(Plane& p) const
{
dp=&p;
errors=p.a+p.b+p.c;// simplified
}
否則,請考慮對訪問者內部的圖形持有非常量“引用”:
struct NVisitor: default_bfs_visitor
{
NGraph* gref_;
NVisitor(NGraph& g) : gref_(&g) {}
void discover_vertex(VertexDesc u, const NGraph& g) const
{
Plane* p = /*get it somewhere*/;
(*gref_)[u].AssignPlane(p);
}
}
注意不要破壞BFS的不變性(例如,遍歷時不要編輯邊)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.