write_graphviz 顶点列表为 setS 时不起作用?

write_graphviz does not work when vertex list is setS?

以下代码有错误:

无法将‘std::basic_ostream’左值绑定到‘std::basic_ostream&&

#include <boost/graph/graphviz.hpp>
void foo(int,char*[])
{
  using namespace boost;

  typedef boost::adjacency_list<
      boost::setS, // outedge list
      boost::setS, // vertex list
      boost::directedS, // undirected 
      boost::no_property, // vertex prop
      boost::no_property, // edge prop
      boost::no_property, // graph prop
      boost::setS // edgelistc
      > Graph;
  Graph g;

  std::ostringstream dotname;
  dotname << "a.dot";
  std::ofstream dot(dotname.str());

  write_graphviz(dot, g);
}

当 boost::vecS, // 顶点列表

是否符合预期?

将顶点容器选择器更改为 setS 将顶点描述符更改为不可流式传输的类型。

与 BGL 中的许多其他算法一样,您应该传递一个单独的顶点索引:

IN: VertexAndEdgeListGraph& g

A directed or undirected graph. The graph's type must be a model of VertexAndEdgeListGraph. In most cases, the graph must have an internal vertex_index property map.

外部顶点 ID 映射

Live On Coliru

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

int main(int,char*[])
{
    using namespace boost;

    typedef boost::adjacency_list<setS, setS, directedS> Graph;
    Graph g;

    std::mt19937 prng{std::random_device{}()};
    generate_random_graph(g, 3, 5, prng);

    std::map<Graph::vertex_descriptor, size_t> ids;

    for (auto u : make_iterator_range(vertices(g)))
        ids[u] = ids.size();

    default_writer w;

    write_graphviz(std::cout, g, w, w, w, make_assoc_property_map(ids));
}

打印例如

digraph G {
1;
2;
3;
1->2 ;
2->1 ;
2->3 ;
3->1 ;
3->2 ;
}

内部属性地图:

您可以将 属性 放在内部而无需太多更改:

Live On Coliru

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

int main(int,char*[])
{
    using namespace boost;

    typedef boost::adjacency_list<setS, setS, directedS, property<vertex_index_t, size_t> > Graph;
    Graph g;

    std::mt19937 prng{std::random_device{}()};
    generate_random_graph(g, 3, 5, prng);

    auto ids = get(vertex_index, g);
    size_t num = 0;
    for (auto u : make_iterator_range(vertices(g)))
        put(ids, u, num++);

    write_graphviz(std::cout, g);
}