如何使用boost make_label_writer编写边缘属性? [英] How to use boost make_label_writer to write edge properties?

查看:163
本文介绍了如何使用boost make_label_writer编写边缘属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的图形,我用顶点确定了写属性,但是当我使用make_label_writer将属性写到边缘时,编译器总是会抱怨.有人可以帮忙吗?

I have a simple graph, I suceeded writing properties with the vertex, but when I use make_label_writer to write properties to the edges, the complier always complains. Could someone help with it?

我的代码如下:

int main (int argc, char * argv[]) {
    typedef std::pair<int ,int> Edge;
    std::vector<Edge> used_by = {Edge(1, 0), Edge(2, 1),
            Edge(1,2), Edge(2, 0)};
    using namespace boost;
    typedef adjacency_list<vecS, vecS, directedS
             > Graph;
    Graph g(used_by.begin(), used_by.end(), 3);
    std::ofstream dmp;
    dmp.open("dmp.dot");
    //name for vertex
    std::vector<std::string> name{"one", "two", "three"};
    //name for edge
    std::vector<std::string> name1{"e1", "e2", "e3", "e4"};
    write_graphviz(std::cout, g, make_label_writer(&name[0]) 
                                   ,make_label_writer(&name1[0]));
}

write_graphviz()将ofc称为模板,这非常好:

The write_graphviz() will ofc called the template, which is perfectly fine :

  template <typename Graph, typename VertexWriter, typename 
  EdgeWriter>
  inline void
  write_graphviz(std::ostream& out, const Graph& g,
             VertexWriter vw, EdgeWriter ew
       BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,vertex_list_graph_tag))
  {
    default_writer gw;
    write_graphviz(out, g, vw, ew, gw);
  }

现在的问题是:当我仅使用make_label_writer(&name[0]]])编写顶点属性时,代码可以完美运行.但是,当我添加make_label_writer(&name1[0])时,会出现错误.

So the problem is now: when I only write the vertex properties using make_label_writer(&name[0]]]), the code runs perfectly. But when I add make_label_writer(&name1[0]), there is error.

推荐答案

默认顶点索引为整数,这就是为什么您可以将第一个顶点名称的地址用作隐式关联属性映射的原因.

The default vertex index is integral, which is why you can use the address of the first vertex name as implied associative property map.

边缘描述符是另一种野兽,需要您选择

The edge descriptor is a different beast and requires you to either

  • 创建一个显式的迭代器属性图(使用额外的索引属性图将边缘描述符映射到name1向量的整数索引)
  • 或使用关联PropertyMap概念的模型.
  • create an explicit iterator property map (using an extra index property map to map from edge descriptor to the integral index into the name1 vector)
  • or use a model of the Associative PropertyMap concept.

在这种情况下,您应该稍后使用std::map<edge_descriptor, std::string>属性.

In this case you should property do the later using a std::map<edge_descriptor, std::string>.

也请考虑使用 在Coliru上直播

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

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS> Graph;

int main() {
    Graph g(3);
    auto e1 = add_edge(1, 0, g).first;
    auto e2 = add_edge(2, 1, g).first;
    auto e3 = add_edge(1, 2, g).first;
    auto e4 = add_edge(2, 0, g).first;

    std::vector<std::string>                      vname{ "one", "two", "three" };
    std::map<Graph::edge_descriptor, std::string> ename{ 
        { e1, "e1" },
        { e2, "e2" },
        { e3, "e3" },
        { e4, "e4" },
    };

    write_graphviz(std::cout, g,
            boost::make_label_writer(&vname[0]),
            make_label_writer(boost::make_assoc_property_map(ename)));
}

打印

在Coliru上直播

#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::vecS, boost::vecS, 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)));
}

打印相同

这篇关于如何使用boost make_label_writer编写边缘属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆