本文介绍了为GraphViz的Boost图捆绑输出重载流运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在Boost图表库中使用捆绑的属性,使用标准库类型,同时还使用< 流运算符的类型重载满足 write_graphviz

Is it possible to use bundled properties in the Boost Graph Library, with a standard library type, while also using that type's overload of the << stream operator to satisfy write_graphviz?

#include <boost/graph/graphviz.hpp>

namespace boost {
  namespace detail {
    namespace has_left_shift_impl {
      template <typename T>
      inline std::ostream &operator<<(std::ostream &o, const std::array<T,2> &a) {
        o << a[0] << ',' << a[1];
        return o;
      }
    }
  }
}

static_assert(boost::has_left_shift<
                std::basic_ostream<char>,
                std::array<double,2>
              >::value,"");

int main()
{
  using namespace boost;
  typedef adjacency_list<vecS, vecS, directedS, no_property, std::array<double,2>> Graph;
  Graph g;
  add_edge(0, 1, {123,456}, g);
  write_graphviz(std::cout, g, default_writer(),
                   make_label_writer(boost::get(edge_bundle,g)));
  return 0;
}

面对Boost静态断言,我修改了我的代码;采纳的建议,其中 <实现在 boost :: detail :: has_left_shift_impl 命名空间中定义。唉,我现在面临另一个错误:

Faced with a Boost static assert, I modified my code to that above; adopting a suggestion from here, wherein the << implementation is defined within the boost::detail::has_left_shift_impl namespace. Alas, I'm now faced with another error:

/usr/include/boost/lexical_cast.hpp:1624:50: error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’
                 bool const result = !(out_stream << input).fail();


code>可以由 write_graphviz 使用?我使用的是Ubuntu 14.10和GCC 4.9.1。

Is there a way to provide an overload of << which can be used by write_graphviz? I'm using Ubuntu 14.10 and GCC 4.9.1.

推荐答案

你甚至不能做

std::cout << g[add_edge(0,1,{123,456},g)];

不提供

inline static std::ostream& operator<<(std::ostream& os, std::array<double, 2> const& doubles) {
    return os << "{" << doubles[0] << "," << doubles[1] << "}";
}



现在得到 lexical_cast 在正确的时间是非常难以移植(主要是因为ADL不会帮助你 std :: array double )。

Now getting these overloads seen by lexical_cast at the right time is exceptionally hard to do portably (mostly because ADL won't help you with std::array and double).

可以使用值转换属性映射(当然是readonly):

Instead you can use a value transforming property map (which is readonly of course):

std::string prettyprint(std::array<double, 2> const& arr) {
    std::ostringstream oss;
    oss << "{" << arr[0] << "," << arr[1] << "}";
    return oss.str();
}

然后 make_transform_value_property_map(prettyprint,get(edge_bundle, g))

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

using namespace boost;

std::string prettyprint(std::array<double, 2> const& arr) {
    std::ostringstream oss;
    oss << "{" << arr[0] << "," << arr[1] << "}";
    return oss.str();
}

int main()
{
    using namespace boost;
    typedef adjacency_list<vecS, vecS, directedS, no_property, std::array<double,2>> Graph;
    Graph g;
    add_edge(0, 1, {123,456}, g);
    write_graphviz(std::cout, g, default_writer(),
            make_label_writer(make_transform_value_property_map(&prettyprint, get(edge_bundle, g))));
}

列印

digraph G {
0;
1;
0->1 [label="{123,456}"];
}

这篇关于为GraphViz的Boost图捆绑输出重载流运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 04:22