我正在查看解决方案here ,这对我不起作用(但请阅读 === 行以实际查看当前问题)。
我尝试过:
boost::undirected_dfs(G, vertex(0,G), boost::visitor(vis));
但我明白了
error C2780: 'void boost::undirected_dfs(const Graph &,const boost::bgl_named_params<P,T,R> &)' : expects 2 arguments - 3 provided
error C2780: 'void boost::undirected_dfs(const Graph &,DFSVisitor,VertexColorMap,EdgeColorMap)' : expects 4 arguments - 3 provided
等等。我有点明白问题是什么(我需要向它传递一些命名参数,但我认为我的图表中没有任何参数。另外,我根本不明白颜色图的处理是什么.
================================================== ===============================
我的图表已定义:
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, boost::no_property, EdgeInfoProperty > Graph;
typedef Graph::edge_descriptor Edge;
typedef Graph::vertex_descriptor Vertex;
我只想做 DFS,至少现在是这样。
所以我把它改成了boost::depth_first_search
,而且似乎有效。
我有(注意 const
与上面链接的解决方案相比缺少 void discover_vertex
):
class MyVisitor : public boost::default_dfs_visitor {
public:
void discover_vertex(Vertex v, const Graph& g) { //note the lack of const
if(boost::in_degree(v,g)!=0){ //only print the vertices in the connected component (I already did MCC and removed edges so all the extra vertices are isolated)
std::cerr << v << std::endl;
vv.push_back(v);
}
return;
}
std::vector<Vertex> GetVector() const { return vv; }
private:
std::vector<Vertex> vv;
};
如果我离开const
我得到error C2663: 'std::vector<_Ty>::push_back' : 2 overloads have no legal conversion for 'this' pointer with [ _Ty=size_t ]
.
现在,这工作正常,或者至少它以正确的顺序打印出正确访问的顶点:
MyVisitor vis;
boost::depth_first_search(G, boost::visitor(vis));
但是当我这样做时:
std::vector<Vertex> vctr = vis.GetVector();
std::cout<<vctr.size();
大小为零,因为我的 vv
没有改变...
那么,当类用作 boost::visitor
的参数时,如何获得适当的类行为? (我什至不确定这是一个合适的问题)。我需要能够改变EdgeInfoProperty
基于之前访问过哪些节点(或者更确切地说,基于 DFS 遍历中哪个顶点是当前顶点的父顶点,因此这可能只是实现这一目标的第一步)。
最佳答案
访问者是按值传递的,因此您需要与复制到函数调用中的 MyVisitor 实例共享它所保存的 vector 。
试试这个:
class MyVisitor : public boost::default_dfs_visitor {
public:
MyVisitor(): vv(new std::vector<Vertex>()){}
void discover_vertex(Vertex v, const Graph& g) { //note the lack of const
if(boost::in_degree(v,g)!=0){ //only print the vertices in the connected component (I already did MCC and removed edges so all the extra vertices are isolated)
std::cerr << v << std::endl;
vv->push_back(v);
}
return;
}
std::vector<Vertex>& GetVector() const { return *vv; }
private:
boost::shared_ptr< std::vector<Vertex> > vv;
};
关于c++ - Boost DFS如何保存访问过的顶点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25166500/