2016-12-08 5 views
-1

注意:以下の質問は「ブーストグラフ」のコンテキストです。しかし、問題はおそらく "C++"の問題または "ブーストグラフ"の問題です。 ブースト・グラフundirected_dfsを使用してundirected_dfs:エッジのベクトルを取得する方法

は、私はエッジのベクトルを取得しようとしています。

コードは正しくback_edgeとtree_edgeを表示します。

私の目標は、エッジのベクトルを取得することです。この操作では、私はエッジのベクトルのベクトルを使用します。

tree_edgeが発見されたときに、私はベクターに格納:back_endが見つかった場合

edgeVisited.push_back(e); 

、Iはベクトルのベクトルに(edgeVisited)エッジのこのベクトルを格納する。

myList.push_back(edgeVisited); 

この操作の直後に、私はmyListのサイズを確認します。結果が正しい:undirected_dfsへの呼び出しの後

std::cout << "myList size by back_edge: " << myList.size() << std::endl; 

、私は

std::vector< std::vector<edge_t> > vctr = vis.GetEdges(); 

ではmyListを取得し、ことにより、サイズをチェックしたい:

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

それは、より "C++" 問題でした。 iはクラスdetect_loop

detect_loops(std::vector< std::vector<edge_t> > &vctr) 

へのエントリーパラメータとしてベクトルのベクトルを追加し、それを呼び出す:

std::vector< std::vector<edge_t> > vctr; 
detect_loops vis(vctr); 

すべてです。ここソリューションです。 ありがとうございました

関連する問題