C++ 具有捆绑属性的Boost BGL read_graphviz

C++ 具有捆绑属性的Boost BGL read_graphviz,c++,boost,graph,graphviz,C++,Boost,Graph,Graphviz,我有一个这样定义的图: struct EdgeInfoProperty{ int score; //is the trans from v to u, where u<v Trans trans; }; typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, boost::no_property, EdgeInfoProperty > Graph; type

我有一个这样定义的图:

struct EdgeInfoProperty{
    int score;
    //is the trans from v to u, where u<v
    Trans trans;
};
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, boost::no_property, EdgeInfoProperty > Graph;
typedef Graph::edge_descriptor Edge;
typedef Graph::vertex_descriptor Vertex;
我的点文件看起来像:

graph G {
0;
1;
2;
3;
4;
5;
6;
7;
8;
9;
10;
11;
12;
0--1 [label="-3"];
0--5 [label="-2"];
2--3 [label="-8"];
3--8 [label="-4"];
4--5 [label="-1"];
4--6 [label="-6"];
4--7 [label="-5"];
4--8 [label="-10"];
8--9 [label="-9"];
}
所以,我基本上想加载G,这样在0--1之间有一条边,以此类推,
G[edge]。分数=-3
etc.和
G[edge]。trans=//一些默认值或任何值

我现在所拥有的东西给了我大量的编译错误,我正在认真考虑只做一个图形的纯文本副本,而不是尝试读取点文件,然后从那里重新创建图形

以下是我所拥有的:

    std::string gn = loc + "MST.dot";
    Graph G(0);
    boost::dynamic_properties dp;
    boost::property_map<Graph, boost::vertex_name_t>::type name = boost::get(boost::vertex_name, G);
    dp.property("node_id",name);

    auto score = boost::get(&EdgeInfoProperty::score, G);
    dp.property("score",score);

    std::filebuf fb;
    fb.open (gn, std::ios::in);
    std::istream isg(&fb);
    bool status = boost::read_graphviz(isg,G,dp,"node_id");
std::string gn=loc+“MST.dot”;
图G(0);
boost::动态_属性dp;
boost::property\u map::type name=boost::get(boost::vertex\u name,G);
dp.属性(“节点id”,名称);
自动评分=boost::get(&EdgeInfo属性::评分,G);
dp.财产(“分数”,分数);
std::filebuf fb;
fb.open(gn,std::ios::in);
标准::istream isg(&fb);
bool status=boost::read_graphviz(isg、G、dp,“节点id”);

我很确定只有分数是个问题,但我尝试了获取EdgeInfo属性映射,只是我尝试的所有操作都出现了错误…

问题

您的代码中有几个错误:

boost::get(boost::vertex_name,G)
假设顶点中有一个名为name的属性,但事实并非如此。此外,
read_graphviz()
将节点标识符存储到名为
node\u id
的属性映射中。但是在你的例子中,你的顶点没有属性。它不能工作

正在运行的示例

#include <iostream>
#include <sstream>

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/property_map/dynamic_property_map.hpp>
using namespace std;

struct Trans {
    int t;
};

struct EdgeInfoProperty {
    int score;
    //is the trans from v to u, where u<v
    Trans trans;
};

struct NodeInfoProperty {
    int index;
};

/* you cannot read a graph without node property. By default, read_graphviz() assume
 * nodes have a "node_id" property map...
 */
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, NodeInfoProperty, EdgeInfoProperty > Graph;

template<class Index> class noeud_writer {
public:
    noeud_writer(Index id) : idm(id){}
    template<class Noeud> void operator()(std::ostream & out, const Noeud & n) {
        out << "[index=" << idm[n] << "]"; }
private:
    Index idm;
};

template< class IdMap> inline noeud_writer<IdMap> make_noeud_writer(IdMap idm) {
    return noeud_writer<IdMap>(idm); }

template<class Score> class edge_writer {
public:
    edge_writer(Score s) : score(s){}
    template<class Noeud> void operator()(std::ostream & out, const Noeud & n) {
        out << "[score=" << score[n] << "]"; }
private:
    Score score;
};

template< class Score> inline edge_writer<Score> make_edge_writer(Score score) {
    return edge_writer<Score>(score); }


int main(int argc, char * argv[])
{
    Graph G(0);
    boost::dynamic_properties dp;
    dp.property("index", boost::get(&NodeInfoProperty::index,G));

    auto score = boost::get(&EdgeInfoProperty::score, G);
    dp.property("score", score);

    std::istringstream isg("graph G {0[index=0];1[index=1];0--1[score=-3];}");
    bool status = boost::read_graphviz(isg, G, dp,"index");

    write_graphviz(std::cout, G, make_noeud_writer(boost::get(&NodeInfoProperty::index, G)),
                                  make_edge_writer(boost::get(&EdgeInfoProperty::score, G)));
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构转换{
int t;
};
结构EdgeInfo属性{
智力得分;

//是从v到u的转换,用户界面最终生成了一个文本文件并读取该文件并使用该信息生成了我的文件,所以我解决了我的问题。不幸的是,我没有办法检查这是否正确(我再也无法访问这些文件了)。我会接受你的答案,只是因为我花了很大的努力来编写它!
#include <iostream>
#include <sstream>

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/property_map/dynamic_property_map.hpp>
using namespace std;

struct Trans {
    int t;
};

struct EdgeInfoProperty {
    int score;
    //is the trans from v to u, where u<v
    Trans trans;
};

struct NodeInfoProperty {
    int index;
};

/* you cannot read a graph without node property. By default, read_graphviz() assume
 * nodes have a "node_id" property map...
 */
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, NodeInfoProperty, EdgeInfoProperty > Graph;

template<class Index> class noeud_writer {
public:
    noeud_writer(Index id) : idm(id){}
    template<class Noeud> void operator()(std::ostream & out, const Noeud & n) {
        out << "[index=" << idm[n] << "]"; }
private:
    Index idm;
};

template< class IdMap> inline noeud_writer<IdMap> make_noeud_writer(IdMap idm) {
    return noeud_writer<IdMap>(idm); }

template<class Score> class edge_writer {
public:
    edge_writer(Score s) : score(s){}
    template<class Noeud> void operator()(std::ostream & out, const Noeud & n) {
        out << "[score=" << score[n] << "]"; }
private:
    Score score;
};

template< class Score> inline edge_writer<Score> make_edge_writer(Score score) {
    return edge_writer<Score>(score); }


int main(int argc, char * argv[])
{
    Graph G(0);
    boost::dynamic_properties dp;
    dp.property("index", boost::get(&NodeInfoProperty::index,G));

    auto score = boost::get(&EdgeInfoProperty::score, G);
    dp.property("score", score);

    std::istringstream isg("graph G {0[index=0];1[index=1];0--1[score=-3];}");
    bool status = boost::read_graphviz(isg, G, dp,"index");

    write_graphviz(std::cout, G, make_noeud_writer(boost::get(&NodeInfoProperty::index, G)),
                                  make_edge_writer(boost::get(&EdgeInfoProperty::score, G)));
    return 0;
}