【问题标题】:Print a constified subgraph with write_graphviz()使用 write_graphviz() 打印一个 constified 子图
【发布时间】:2012-03-12 14:37:44
【问题描述】:

我正在努力将图表转储到流中,其中所述图表是 boost::subgraph 的 constified 版本。

我尝试提供property writer,但基本上它失败了,因为它似乎需要一个方法boost::get(PropertyWriter, VertexDescriptor)。使用图表不是的相同方法,subgraph 可以按预期工作。

作为found here,我必须使用boost::dynamic_properties(参见下面的代码),但是当我的图表不可写时它会失败(而the documentation 表示该图表被视为一个约束参考)。

这是一个我无法开始工作的简单示例:

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

struct VertexProperties
{
  std::string name;
};

int main()
{
  typedef boost::subgraph<
    boost::adjacency_list<
      boost::vecS, boost::vecS,
      boost::directedS,
      boost::property<boost::vertex_index_t, std::size_t, VertexProperties>,
      boost::property<boost::edge_index_t, std::size_t> > > Graph;

  Graph const g;

  boost::dynamic_properties dp;
  dp.property("name", boost::get(&VertexProperties::name, g));
  dp.property("node_id", boost::get(boost::vertex_index, g));
  boost::write_graphviz_dp(std::cout, g, dp);
}

欢迎任何提示!非常感谢,


编辑

在我的情况下,我忘了提及“失败”是什么意思;这是我尝试编译时的错误:

错误:将 'const std::basic_string' 作为 'std::basic_string<_chart _traits _alloc> 和 std::basic_string<_chart _traits _alloc>::operator=(const std ::basic_string<_chart _traits _alloc>&) [其中 _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator, std::basic_string<_chart _traits _alloc> = std::basic_string]'丢弃限定符 [-fpermissive]

【问题讨论】:

  • 能否请您在 Boost 错误跟踪器中提交一个关于此的错误?它应该可以工作。
  • 谢谢,我按照你的建议做了。我还发布了一个“答案”,它提供了一种解决方法。

标签: c++ boost graph graphviz boost-graph


【解决方案1】:

按照建议,我报告这是 Boost.Graph (see the ticket) 中的一个错误。

作为一种解决方法,似乎可以通过访问公共范围内的成员 m_graph 来使用基础图而不是子图。

以下是我使用属性编写器解决问题的方法:

struct VertexProperties
{
  std::string name;
};

template <typename Graph> struct prop_writer
{
  prop_writer(Graph const& g): g_(g) {}

  template <typename Vertex> void operator()(std::ostream& out, Vertex v) const
  {
    out << g_[v].name;
  }

  Graph const& g_;
}

typedef boost::subgraph<
  boost::adjacency_list<
    boost::vecS, boost::vecS,
    boost::directedS,
    boost::property<boost::vertex_index_t, std::size_t, VertexProperties>,
    boost::property<boost::edge_index_t, std::size_t> > > Graph;

Graph const g;

// Note the use of g.m_graph here.
boost::write_graphviz(std::cout, g.m_graph, prop_writer<Graph>(g));

【讨论】:

    猜你喜欢
    • 2015-06-01
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多