为什么不能在 BGL 的访问者中更改边缘属性?
Why edge properties can't be changed within visitor in BGL?
我想在检查边缘时更改边缘权重,但它告诉
error: assignment of member ‘EdgeProperty::weight’ in read-only object g[e].weight = 1/g[e].residual_capacity;
是否有办法在自定义访问者的函数中更改边缘属性?谢谢
struct EdgeProperty{
float weight;
float capacity;
float residual_capacity;
};
class custom_dijkstra_visitor : public boost::default_dijkstra_visitor
{
public:
template < typename Edge, typename Graph >
void examine_edge(Edge e, Graph const & g)
{
g[e].weight = 1/g[e].residual_capacity;
}
};
最后我通过在访问者中存储一个Graph
指针解决了这个问题。
class custom_dijkstra_visitor : public boost::default_dijkstra_visitor
{
public:
template < typename Edge, typename Graph >
void examine_edge(Edge e, Graph & g)
{
(*gg)[e].weight = 1/(*gg)[e].residual_capacity;
}
Graph* gg = nullptr;
};
Finally I solved it by storing a Graph pointer in the visitor.
请注意,您可能会将访问者传递给算法。该算法有自己的不变量。如果你改变重量,例如dijkstra_shortest_paths
可能根本不会终止,或者产生不正确(次优)的结果。
如果您想根据先前算法的结果更改权重,并且算法 使用 (取决于)权重,最安全的做法是存储"updated" 权重在单独的地图中,然后应用更改。
我想在检查边缘时更改边缘权重,但它告诉
error: assignment of member ‘EdgeProperty::weight’ in read-only object g[e].weight = 1/g[e].residual_capacity;
是否有办法在自定义访问者的函数中更改边缘属性?谢谢
struct EdgeProperty{
float weight;
float capacity;
float residual_capacity;
};
class custom_dijkstra_visitor : public boost::default_dijkstra_visitor
{
public:
template < typename Edge, typename Graph >
void examine_edge(Edge e, Graph const & g)
{
g[e].weight = 1/g[e].residual_capacity;
}
};
最后我通过在访问者中存储一个Graph
指针解决了这个问题。
class custom_dijkstra_visitor : public boost::default_dijkstra_visitor
{
public:
template < typename Edge, typename Graph >
void examine_edge(Edge e, Graph & g)
{
(*gg)[e].weight = 1/(*gg)[e].residual_capacity;
}
Graph* gg = nullptr;
};
Finally I solved it by storing a Graph pointer in the visitor.
请注意,您可能会将访问者传递给算法。该算法有自己的不变量。如果你改变重量,例如dijkstra_shortest_paths
可能根本不会终止,或者产生不正确(次优)的结果。
如果您想根据先前算法的结果更改权重,并且算法 使用 (取决于)权重,最安全的做法是存储"updated" 权重在单独的地图中,然后应用更改。