C++BOOST库和捆绑属性

C++ BOOST library and bundled properties

本文关键字:属性 C++BOOST      更新时间:2023-10-16

我正试图使用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的概念pont)

注意:我使用的是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);