Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无向_dfs:如何获取边的向量_C++_Boost Graph - Fatal编程技术网

C++ 无向_dfs:如何获取边的向量

C++ 无向_dfs:如何获取边的向量,c++,boost-graph,C++,Boost Graph,NB:下面的问题是“推进图”的上下文。但问题可能是“c++”问题或“boost graph”问题。 使用boost图和无向图,我试图得到边的向量 代码正确地打印背面边缘和树边缘 我的目标是检索边的向量。对于这个操作,我使用边向量的向量 因此,当找到树的边时,我将其存储在向量中: edgeVisited.push_back(e); 找到后端后,我将此边向量(edgeVisited)存储在向量向量中: myList.push_back(edgeVisited); 在这个操作之后,我检查myLis

NB:下面的问题是“推进图”的上下文。但问题可能是“c++”问题或“boost graph”问题。

使用boost图无向图,我试图得到边的向量

代码正确地打印背面边缘和树边缘

我的目标是检索边的向量。对于这个操作,我使用边向量的向量

因此,当找到树的边时,我将其存储在向量中:

edgeVisited.push_back(e);
找到后端后,我将此边向量(edgeVisited)存储在向量向量中:

myList.push_back(edgeVisited);
在这个操作之后,我检查myList的大小。结果是正确的:

std::cout << "myList size by back_edge: " << myList.size() << std::endl;
std::cout这更像是一个“c++”问题。以下是解决方案:
我将向量向量作为入口参数添加到类detect_循环中

detect_loops(std::vector< std::vector<edge_t> > &vctr )
检测循环(std::vector&vctr)
并称之为:

std::vector< std::vector<edge_t> > vctr;
detect_loops vis(vctr);
std::vectorvctr;
检测_环vis(vctr);
就这些。 谢谢

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;
}
detect_loops(std::vector< std::vector<edge_t> > &vctr )
std::vector< std::vector<edge_t> > vctr;
detect_loops vis(vctr);