注意:下面的问题是“升级图”的背景。但问题可能是“c ++”问题或“提升图”问题。
使用 boost-graph 和 undirected_dfs ,我试图获取边缘向量。
代码正确打印back_edge和tree_edge。
我的目标是检索边缘矢量。对于此操作,我使用边向量矢量。
因此,当找到tree_edge时,我将其存储在向量中:
edgeVisited.push_back(e);
当找到back_end时,我将这个边缘向量(edgeVisited)存储在向量向量中:
myList.push_back(edgeVisited);
在此操作之后,我检查myList的大小。结果是正确的:
std::cout << "myList size by back_edge: " << myList.size() << std::endl;
在调用undirected_dfs之后,我希望通过
获取myListstd::vector< std::vector<edge_t> > vctr = vis.GetEdges();
并通过以下方式检查尺寸:
std::cout << vctr.size() << std::endl;
但是矢量无效。
能帮助我理解为什么这个向量向量是空的吗?
Here is the whole code:
#include <iostream>
#include <string>
#include <boost/cstdlib.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/undirected_dfs.hpp>
#include <boost/graph/graphviz.hpp>
using namespace boost;
typedef adjacency_list<
vecS,
vecS,
undirectedS,
no_property,
property<edge_color_t, default_color_type> > graph_t;
typedef boost::graph_traits<graph_t>::vertex_descriptor vertex_t;
typedef boost::graph_traits < graph_t>::edge_descriptor edge_t;
struct detect_loops : public boost::dfs_visitor<>
{
template <class edge_t, class Graph>
void back_edge(edge_t e, const Graph& g) {
std::cout << source(e, g) << " -- " << target(e, g) << "\n";
edgeVisited.push_back(e);
myList.push_back(edgeVisited);
edgeVisited.clear();
std::cout << "myList size by back_edge: " << myList.size() << std::endl;
}
template <class Graph>
void tree_edge(edge_t e, const Graph& g) {
std::cout << "tree_edge: " << boost::source(e, g) << " --> " << boost::target(e, g) << std::endl;
edgeVisited.push_back( e );
}
//get the vectors.
std::vector< std::vector<edge_t> > GetEdges() const {
std::cout << "MyList by GetEdges : " << myList.size() << std::endl;
return myList;
}
private:
std::vector<edge_t> edgeVisited;
std::vector< std::vector<edge_t> > myList;
};
void make(graph_t &g)
{
//Create the graph
boost::add_edge(0, 1, g);
boost::add_edge(0, 2, g);
boost::add_edge(1, 3, g);
boost::add_edge(2, 3, g);
boost::add_edge(2, 4, g);
boost::add_edge(3, 5, g);
boost::add_edge(4, 5, g);
//print the graph
std::ofstream f("d:\\tmp\\dot\\s13.dot");
boost::write_graphviz(f, g);
std::system(std::string("dot -Tsvg -Grankdir=LR -Nfontsize=24 d:\\tmp\\dot\\s13.dot > d:\\tmp\\dot\\s13.svg").c_str());
}
int main(int, char*[])
{
graph_t g;
make(g);
detect_loops vis;
undirected_dfs(g, root_vertex(vertex_t(0)).visitor(vis) .edge_color_map(get(edge_color, g)));
std::vector< std::vector<edge_t> > vctr = vis.GetEdges();
std::cout << vctr.size() << std::endl;
return boost::exit_success;
}
由于
答案 0 :(得分:0)
这更像是一个&#34; c ++&#34;问题。这是解决方案: 我将矢量矢量作为入口参数添加到类detect_loop
detect_loops(std::vector< std::vector<edge_t> > &vctr )
并称之为:
std::vector< std::vector<edge_t> > vctr;
detect_loops vis(vctr);
这就是全部。 感谢