Boost BGL:用于绑定属性和列表(如VertexList/EdgeList)的自定义属性编写器

Boost BGL:用于绑定属性和列表(如VertexList/EdgeList)的自定义属性编写器,boost,boost-graph,Boost,Boost Graph,我从post中获取了以下代码,并将VertexList和EdgelList更改为List,而不是vecS 我发现由于缺少索引make\u label\u writer(get(&VertexProps::name,g))无法工作。有人能告诉我如何更改代码以使其正常工作吗。我更喜欢使用自定义属性编写器的解决方案 多谢各位 #include <boost/graph/adjacency_list.hpp> #include <boost/graph/graphviz.hpp>

我从post中获取了以下代码,并将VertexList和EdgelList更改为List,而不是vecS 我发现由于缺少索引
make\u label\u writer(get(&VertexProps::name,g))
无法工作。有人能告诉我如何更改代码以使其正常工作吗。我更喜欢使用自定义属性编写器的解决方案 多谢各位

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>

struct VertexProps { std::string name; };
struct EdgeProps   { std::string name; };
typedef boost::adjacency_list<boost::listS, boost::listS, boost::directedS, VertexProps, EdgeProps> Graph;

int main() {
    Graph g(3);
    g[0].name = "one";
    g[1].name = "two";
    g[2].name = "three";
    add_edge(1, 0, {"e1"}, g);
    add_edge(2, 1, {"e2"}, g);
    add_edge(1, 2, {"e3"}, g);
    add_edge(2, 0, {"e4"}, g);

write_graphviz(std::cout, g,
        make_label_writer(get(&VertexProps::name, g)),
        make_label_writer(get(&EdgeProps::name, g)));
#包括
#包括
结构VertexProps{std::string name;};
结构EdgeProps{std::string name;};
typedef boost::邻接列表图;
int main(){
图g(3);
g[0].name=“一”;
g[1].name=“两个”;
g[2].name=“三”;
加_边(1,0,{“e1”},g);
加_边(2,1,{“e2”},g);
加_边(1,2,{“e3”},g);
加_边(2,0,{“e4”},g);
书写图形(标准::cout,g,
制作标签编写器(get(&VertexProps::name,g)),
制作标签(get(&EdgeProps::name,g));
}


为了完整起见,下面是使用自定义属性编写器对解决方案的修改。请别忘了对原始答案投赞成票

#包括
#包括
#包括
结构VertexProps{std::string name;};
结构EdgeProps{std::string name;};
typedef boost::邻接列表图;
模板
为我的标签编剧分类{
公众:
我的作者(姓名):姓名({}
模板
void运算符()(std::ostream&out,const VertexOrEdge&v)const{

out使用
列表
顶点描述符类型不是整数,因此不适合作为顶点索引

您现在需要使用实际的描述符:

Graph g;
Graph::vertex_descriptor v0 = add_vertex({"one"}, g);
Graph::vertex_descriptor v1 = add_vertex({"two"}, g);
Graph::vertex_descriptor v2 = add_vertex({"three"}, g);
add_edge(v1, v0, {"e1"}, g);
add_edge(v2, v1, {"e2"}, g);
add_edge(v1, v2, {"e3"}, g);
add_edge(v2, v0, {"e4"}, g);
此外,在编写时,您还必须提供一个
vertex\u index
属性映射。这顺便要求您传递一个
graph\u property\u writer
形式:

std::map<Graph::vertex_descriptor, int> vertex_index;
for (auto vd : boost::make_iterator_range(vertices(g)))
    vertex_index[vd] = vertex_index.size();

write_graphviz(std::cout, g,
    make_label_writer(get(&VertexProps::name, g)),
    make_label_writer(get(&EdgeProps::name, g)),
    boost::default_writer{}, // graph_property_writer
    boost::make_assoc_property_map(vertex_index));

也请看真棒!非常感谢。顶点索引属性映射正是我需要的提示。
std::map<Graph::vertex_descriptor, int> vertex_index;
for (auto vd : boost::make_iterator_range(vertices(g)))
    vertex_index[vd] = vertex_index.size();

write_graphviz(std::cout, g,
    make_label_writer(get(&VertexProps::name, g)),
    make_label_writer(get(&EdgeProps::name, g)),
    boost::default_writer{}, // graph_property_writer
    boost::make_assoc_property_map(vertex_index));
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>

struct VertexProps { std::string name; };
struct EdgeProps   { std::string name; };
typedef boost::adjacency_list<boost::listS, boost::listS, boost::directedS, VertexProps, EdgeProps> Graph;

int main() {
    Graph g;
    Graph::vertex_descriptor v0 = add_vertex({"one"}, g);
    Graph::vertex_descriptor v1 = add_vertex({"two"}, g);
    Graph::vertex_descriptor v2 = add_vertex({"three"}, g);
    add_edge(v1, v0, {"e1"}, g);
    add_edge(v2, v1, {"e2"}, g);
    add_edge(v1, v2, {"e3"}, g);
    add_edge(v2, v0, {"e4"}, g);

    std::map<Graph::vertex_descriptor, int> vertex_index;
    for (auto vd : boost::make_iterator_range(vertices(g)))
        vertex_index[vd] = vertex_index.size();

    write_graphviz(std::cout, g,
        make_label_writer(get(&VertexProps::name, g)),
        make_label_writer(get(&EdgeProps::name, g)),
        boost::default_writer{}, // graph_property_writer
        boost::make_assoc_property_map(vertex_index));
}
digraph G {
0[label=one];
1[label=two];
2[label=three];
1->0 [label=e1];
1->2 [label=e3];
2->1 [label=e2];
2->0 [label=e4];
}