C ++ BOOST库和捆绑属性

时间:2015-02-26 20:36:26

标签: c++ qt boost graph mining

我试图使用Boost制作图形挖掘程序,所以我从图形结构开始,这是我做的代码:

#include <iostream>
#include <boost/graph/adjacency_list.hpp>
using namespace std;
using namespace boost;



//vertex
struct VertexProperties
{
    int id;
    int label;
    VertexProperties()= default;
    VertexProperties(unsigned i, unsigned l) : id(i), label(l) {}
};



//edge
struct EdgeProperties
{
    unsigned id;
    unsigned label;
    EdgeProperties()= default;
    EdgeProperties(unsigned i, unsigned l) : id(i), label(l) {}
};

//Graph
struct GraphProperties
{
    unsigned id;
    unsigned label;
    GraphProperties()= default;
    GraphProperties(unsigned i, unsigned l) : id(i), label(l) {}
};


//adjency list
typedef boost::adjacency_list<
    boost::vecS, boost::vecS, boost::directedS,
    VertexProperties,
    EdgeProperties,
    GraphProperties
> Graph;


//iterators
typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t;
typedef boost::graph_traits<Graph>::edge_descriptor edge_t;
/***********************************************/
int main()
{
    Graph g;
    vertex_t v1 = boost::add_vertex(VertexProperties(1,10),g);
    vertex_t v2 = boost::add_vertex(VertexProperties(2,20),g);
    //edge_t e1= boost::add_edge(EdgeProperties(3,55),g);
    std::cout << "Vertice: " <<num_vertices(g) << std::endl;
    std::cout << "edges: " <<num_edges(g) << std::endl;

    return 0;
}

这一行存在问题:

edge_t e1= boost::add_edge(EdgeProperties(3,55),g);

如何创建此边缘? PS:请告诉代码是否正确(我的意思是vue的概念)

1 个答案:

答案 0 :(得分:1)

注意:我正在使用GCC 4.8(带-std = c ++ 11标志)和Boost 1.48。

我在您的代码中看到了两个问题。首先,传递给boost :: add_edge(...)的前两个参数应该是与边相关联的顶点。所以在你的情况下,呼叫将是

edge_t e1= boost::add_edge(v1, v2, EdgeProperties(3,55),g);

其次,e1的类型据我所知应该是

std::pair<edge_descriptor, bool>

所以你的第二个typedef将是

typedef std::pair<boost::graph_traits<Graph>::edge_descriptor, bool> edge_t;

或者,您可以使用auto关键字来描述e1的类型。例如,

auto e1= boost::add_edge(v1, v2, EdgeProperties(3,55),g);