2

boost::graphアルゴリズムの使用法を新しい実装クラスのセットに変換するのに苦労しています。私は疑問に思っています:参照boost::graphのみを保存する場合、オブジェクトのプロパティにアクセスすることさえ可能std::shared_ptrですか? 次のように:

class Vert { 
public:
    Vert();
    Vert(std::string n);
    std::string getName() const;
    void setName( std::string const& n );
private:
    std::string name; 

};
typedef std::shared_ptr<Vert> Vert_ptr;

using namespace boost;
typedef boost::adjacency_list<vecS, vecS, directedS, Vert_ptr> Graph;
Graph g;
Vert_ptr a( new Vert("a"));
add_vertex( a, g );
std::ofstream dot("test.dot");
write_graphviz( dot, g, make_label_writer(boost::get(&Vert::getName,g))); //ERROR!

std::shared_ptr実装のグラフ ラベル ライターwrite_graphvizまたはその他のプロパティで使用する のメンバーにアクセスすることは可能ですか?

ありがとうございました!

4

1 に答える 1

3

ええ、変換プロパティマップを使用してください

Live On Coliru

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/property_map/transform_value_property_map.hpp>
#include <fstream>
#include <memory>

using namespace boost;

class Vert { 
public:
    Vert(std::string n="") : name(n) { }
    std::string getName() const { return name; }
    void setName( std::string const& n ) { name = n; }
private:
    std::string name; 
};

typedef std::shared_ptr<Vert> Vert_ptr;

struct Name { std::string operator()(Vert_ptr const& sp) const { return sp->getName(); } };

int main() {
    typedef boost::adjacency_list<vecS, vecS, directedS, Vert_ptr> Graph;
    Graph g;
    Vert_ptr a( new Vert("a"));
    add_vertex( a, g );
    std::ofstream dot("test.dot");
    auto name = boost::make_transform_value_property_map(Name{}, get(vertex_bundle,g));
    write_graphviz( dot, g, make_label_writer(name));
}

結果:

digraph G {
0[label=a];
}
于 2015-11-16T10:55:43.397 に答える