简体   繁体   English

在BOOST GRAPH中定义图后,在构造函数中初始化filter_graph对象

[英]To initialize a filtered_graph object in constructor after graph is defined in BOOST GRAPH

I have a query in initializing filtered_graph in boost. 我在初始化boost中的filter_graph有一个查询。

here is my code, 这是我的代码,

 // predicate 
 template <typename TGraph>
 struct edge_predicate
 {
   bool operator()(const typename boost::graph_traits<TGraph>::edge_descriptor& v) const
   {
     return true //testing purpose
    }
  };

// class 
class A{

 typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, Node_Info, Edge_Info > Graph_t;
 typedef boost::filtered_graph < Graph_t , edge_predicate > FilteredGraphType_t;

 // members
 Graph G;
 FilteredGraphType_t FG() ; // empty filter graph ???

 // constructor for A
 template < ..typename list > 
 A (  ... input iterators list...  )
 {
    fill the graph G with some values passed in constructor argument list.

    // predicate
    edge_predicate< Graph_t >  EPred;

    //filtered_graph
    FilteredGraphType_t FG( G, EPred);   // I am passing on edge predicate.
 }

};

// member function:
FilteredGraphType_t&   get_filtered_graph() 
{   
   return FG; 
 }

Problem: 问题:

FG is a member of class A, and G is also a member of A. Initially G is empty object, so is FG I suppose,.. FG是A类的成员,G也是A的成员。最初,G是空对象,所以我想FG也是如此。

In constructor of A, I fill Graph G with my logic ( such as number of vertices, edges etc ). 在A的构造函数中,我用自己的逻辑(例如,顶点数,边数等)填充GraphG。 Now, I want to create a filter FG (which is member of class A) over graph G (after G is filled). 现在,我想在图G(填充G之后上创建一个过滤器FG (它是A类的成员 )。

The reason I want this is that this filtered graph is passed as a reference to some other class constructor. 我想要这个的原因是,此筛选后的图形作为对其他类构造函数的引用传递。 This forces me to make FG as member of class A. ( I could create new instance of filter graph in constructor of A itself, but it wont serve the purpose of returning the reference to FG. ) 这迫使我使FG成为类A的成员。(我可以在A本身的构造函数中创建过滤器图的新实例,但不会达到将引用返回给FG的目的。)

I hope it is clear. 我希望这很清楚。 please suggest 请建议

You can make your code compile and work OK. 您可以使代码编译并正常运行。 Try reorganizing the constructor as follows: 尝试按如下所示重新组织构造函数:

// constructor for A
 template < ..typename list > 
 A (  ... input iterators list...  )
      : G() //calls constructor for G
      , EPred(...) //some constructor for predicate
      , FG( boost::make_filtered_graph(G, EPred) ) //creates your filtered graph
{
// fill the graph G with some values passed in constructor argument list.
}

The rest of code can remain unchanged. 其余代码可以保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM