remdezx
remdezx

Reputation: 2947

edge_descriptor mapping in bgl

I'm new to BGL and I have a problem with making own property maps which key is edge_property

can you tell me what I'm doing wrong that following code prints not : (0,1) == (0,1) ? 1 but (0,1) == (0,1) ? 0

Here is the code

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

using namespace std;


class A {};
class B {};

typedef boost::adjacency_list<boost::listS, boost::vecS, boost::bidirectionalS,
                                    A, B > Graph;

typedef boost::graph_traits<Graph>::vertex_descriptor vertex_descriptor;
typedef boost::graph_traits<Graph>::edge_descriptor edge_descriptor;
typedef boost::graph_traits<Graph>::edge_iterator edge_iterator;
typedef boost::graph_traits<Graph>::in_edge_iterator in_edge_iterator;
typedef boost::graph_traits<Graph>::out_edge_iterator out_edge_iterator;

map<edge_descriptor, int>  fun(Graph g){

    map<edge_descriptor, int> m;
    m[*(edges(g).first)] = 5;
    return m;
}


int main(){
    Graph g;

    vertex_descriptor a = add_vertex(g);
    vertex_descriptor b = add_vertex(g);

    add_edge(a, b, g);

    map<edge_descriptor, int> m = fun(g);

    edge_iterator ei, ei_end;
    for(tie(ei, ei_end) = edges(g) ; ei !=ei_end ; ++ei){
        cout << m.begin()->first << " == " << *ei << " ? " << (m.begin()->first == *ei) << endl;
    }
}

thank you very much!

EDIT: maybe there is a better way to map edge than by edge_property value?

Upvotes: 3

Views: 754

Answers (1)

user1252091
user1252091

Reputation:

Your edge descriptor has 3 members: source node, target node and a pointer to your B property. In your fun you make a copy of your graph, and the copied edge in your new graph points to a different B. If you declare your fun as map<edge_descriptor, int> fun(const Graph& g) (and you probably should since a copy of a bigger graph can be expensive) you obtain the result you expect.

Upvotes: 1

Related Questions