C++ Boost Graph Library: outputting custom vertex properties

别说谁变了你拦得住时间么 提交于 2019-12-04 14:49:38

I won't correct your code, because I'm not able to verify that it will work as expected. But since I got stuck at the same problem, I will post the relevant parts of my code as an example for you and others. I hope this might be helpful.

Definition of graph

typedef boost::adjacency_list<boost::vecS, 
                              boost::vecS, 
                              boost::bidirectionalS, 
                              boost::no_property, 
                              EdgeProp, //this is the type of the edge properties
                              boost::no_property, 
                              boost::listS> Graph;

Edge Properties

struct EdgeProp
{
        char name;
        //...

};

property writer for edges

template <class Name>
class myEdgeWriter {
public:
     myEdgeWriter(Name _name) : name(_name) {}
     template <class VertexOrEdge>
     void operator()(std::ostream& out, const VertexOrEdge& v) const {
            out << "[label=\"" << name[v].name << "\"]";
     }
private:
     Name name;
};

The properties have to be attached to the edge in advance. e.g

EdgeProp p;
p.name = 'a';
g[edge_descriptor] = p;

Call to boost to create graphviz file

myEdgeWriter<Graph> w(g);
ofstream outf("net.gv");
boost::write_graphviz(outf,g,boost::default_writer(),w);

For the vertex property writer we just use the default writer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!