NB: The question below is the context of "boost-graph". But the issue is maybe a "c++" issue or "boost-graph" issue.
Using boost-graph and undirected_dfs, I am trying to get the vectors of edges.
The code print correctly back_edge and tree_edge.
My goal is to retrieve the vectors of edges. For this operation i use a vector of vectors of edges.
So, when a tree_edge is found i store it in a vector:
edgeVisited.push_back(e);
When the back_end is found, i store this vector of edges (edgeVisited) in a vector of vectors:
myList.push_back(edgeVisited);
Just after this operation, i check the size of myList. The result is correct:
std::cout << "myList size by back_edge: " << myList.size() << std::endl;
After the call to undirected_dfs, i want to get the myList by
std::vector< std::vector<edge_t> > vctr = vis.GetEdges();
and check it size by:
std::cout << vctr.size() << std::endl;
But the vector is void.
Could you please help me to understand why this vector of vector is null?
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;
}
Thanks