修改访问者的捆绑属性

时间:2009-10-02 17:45:08

标签: c++ boost-graph

如何从访问者内部修改顶点的捆绑属性?

我想使用简单的子脚本编写方法,但传递给访问者的图形参数是const,因此编译器不允许更改。

我可以在访问者中存储对图表的引用,但这看起来很奇怪。

/**

  A visitor which identifies vertices as leafs or trees

*/
class bfs_vis_leaf_finder:public default_bfs_visitor {

public:
    /**

    Constructor

    @param[in] total reference to int variable to store total number of leaves
    @param[in] g reference to graph ( used to modify bundled properties )

    */
    bfs_vis_leaf_finder( int& total, graph_t& g ) :
      myTotal( total ), myGraph( g )
      {
          myTotal = 0;
      }

    /**

    Called when the search finds a new vertex

    If the vertex has no children, it is a leaf and the total leaf count is incremented

    */
    template <typename Vertex, typename Graph>
    void discover_vertex( Vertex u, Graph& g)
    {
        if( out_edges( u, g ).first == out_edges( u, g ).second ) {
            myTotal++;
            //g[u].myLevel = s3d::cV::leaf;
            myGraph[u].myLevel = s3d::cV::leaf;
        } else {
            //g[u].myLevel = s3d::cV::tree;
            myGraph[u].myLevel = s3d::cV::tree;
        }
    }

    int& myTotal;
    graph_t& myGraph;
};

2 个答案:

答案 0 :(得分:4)

你的解决方案是正确的。

要将图表类型与访问者分离,您只能将有趣的属性映射传递给访问者构造函数,并使用boost::get(property, u) = s3d::cV::leaf;访问其元素。通过这种方式,您可以将任何类型兼容的顶点属性传递给访问者(访问者将更加通用,并且无法理解图表类型中的名称更改)。

属性映射的类型将是访问者类的模板类型名称,如下所示:

typedef property_map<graph_t, s3d_cv3_leaf_t your_vertex_info::*>::type your_property_map;

有关捆绑属性的完整论文,请参阅here

HTH

答案 1 :(得分:0)

我只是在学习这些东西,但我认为你必须在访问者中存储对图表的引用是正确的。我不确定是不是因为这个原因,但可能是因为他们不想提供所有函数/需要派生的两个版本来提供每个函数的两个版本。特别是当图表变通方法中的传递可用时。

即使感觉很奇怪,我认为传递给图表的参考可能是“正确的方式”。

相关问题