Boost DFS如何保存访问过的顶点
Boost DFS how to save visited vertices?
我正在查看这里的解决方案,它对我不起作用(但请阅读===行下面的内容以实际查看当前问题)。
我试着:
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
,它似乎工作。
我有(注意void discover_vertex
与上面链接的解决方案相比缺乏const
):
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遍历中当前顶点的父顶点,所以这可能只是朝着那个方向迈出的第一步)。
visitor是按值传递的,因此需要与复制到函数调用中的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;
};
- 如何循环打印顶点结构
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- 用于访问容器<T>数据成员的正确 API
- BGL:无法访问捆绑的顶点属性
- 无法使用Assimp访问3D模型(.OBJ)的正确数量的顶点
- CGAL:加载.off文件后,如何访问顶点颜色
- 为什么我的顶点缓冲区对象出现访问冲突错误?
- 在 OpenGL 中,我可以访问我在顶点着色器中绘制的缓冲区吗?
- 使用Boost :: Graph随机访问顶点
- 尝试访问 CGAL 多面体顶点
- 访问网格的顶点
- 如何访问图中顶点边缘的数据
- C++ 提升图形库:构建在无向图形搜索中访问的顶点的向量
- 在boost中访问顶点时出现分割错误
- DFS访问器不遍历分离的顶点
- Boost DFS如何保存访问过的顶点
- 顶点缓冲区的直接状态访问
- 如何找到路径访问尽可能多的顶点