5
4

1 回答 1

4

Take a look at this code, I believe it explains a few things of its own:

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/properties.hpp>
#include <iostream>

namespace bgl = boost;

struct EdgeInfo
{
    int weight;
    float e1;
    float e2;
};

struct EdgeInfoPropertyTag
{
    typedef bgl::edge_property_tag kind;
    static std::size_t const num; // ???
};

std::size_t const EdgeInfoPropertyTag::num = (std::size_t)&EdgeInfoPropertyTag::num;

typedef bgl::property<EdgeInfoPropertyTag, EdgeInfo> edge_info_prop_type;
typedef bgl::adjacency_list<bgl::vecS, bgl::vecS, bgl::bidirectionalS,
    bgl::no_property, edge_info_prop_type> Graph;
typedef bgl::graph_traits<Graph>::vertex_descriptor vertex_descr_type;
typedef bgl::graph_traits<Graph>::edge_descriptor edge_descr_type;

int
main ()
{
    Graph g;
    vertex_descr_type u, v;
    u = add_vertex (g);
    v = add_vertex (g);
    EdgeInfo props;
    props.weight = 3;
    std::pair<edge_descr_type, bool> result = add_edge (u, v, props, g);

    EdgeInfo p = get (EdgeInfoPropertyTag (), g, result.first);
    std::cout << "weight: " << p.weight << std::endl;
}

You need to read about the concepts that BGL is based upon.

This way you can hang any kind of value off of the edge (and similarly for vertex). You could also use the predefined types of properties, like edge_weight_t or edge_name_t I believe.

See also BGL docs about custom edge properties.

于 2011-10-31T12:54:21.193 回答