gpt4 book ai didi

c++ - boost C++ 图 write_graphviz_dp

转载 作者:行者123 更新时间:2023-11-30 02:31:31 25 4
gpt4 key购买 nike

问题是,我无法使用 write_graphviz_dp 编写自定义创建的图形。

我已经创建了一个带有自定义属性的图表:

typedef enum { UNSET = 0, RED = 1, GREEN = 2, BLUE = 3} Color;

enum vertex_one_t { vertex_one };
namespace boost {
BOOST_INSTALL_PROPERTY(vertex, one);
}

enum vertex_two_t { vertex_two };
namespace boost {
BOOST_INSTALL_PROPERTY(vertex, two);
}

enum vertex_three_t { vertex_three };
namespace boost {
BOOST_INSTALL_PROPERTY(vertex, three);
}

typedef boost::property< boost::vertex_index_t, int,
boost::property< vertex_one_t, Color*,
boost::property< vertex_two_t, Color*,
boost::property< vertex_three_t, Color* > > > > DualVertexProperty;

我正在尝试使用

编写图形
// Graph structure with dynamic property output
template<typename Graph>
void
write_graphviz_dp(std::ostream& out, const Graph& g,
const dynamic_properties& dp,
const std::string& node_id = "node_id");

但我在 boost::dynamic_properties 创建 期间遇到错误。

boost::dynamic_properties dpDual;

boost::property_map<DualGraph, vertex_index_t>::type vIndex = get(boost::vertex_index, g);
boost::property_map<DualGraph, vertex_one_t>::type vOne = get(vertex_one, g);

dpDual.property("node_id", vIndex);
dpDual.property("Color_1", get(vOne, g));

更具体地说,编译错误在最后一行。我没有发布错误消息,因为它太长了。

最佳答案

首先,您必须将属性映射传递给 dynamic_properties:

dpDual.property("Color_1", vOne); // or maaaaybe
dpDual.property("Color_1", boost::get(vertex_one, g));

其次,Graphviz 本质上是一种文本格式。您是否为 Color* 定义了文本转换?如果不是,您希望如何呈现属性?

简单的解决方案

为什么属性是指针? Color*Color 更浪费(int 可能比指针小)。

你可以抛开所有的生活问题,实现你的愿望:

inline static std::ostream& operator<<(std::ostream& os, Color color) {
return os << static_cast<int>(color);
}

inline static std::istream& operator>>(std::istream& is, Color& color) {
int dummy;
is >> dummy;
color = static_cast<Color>(dummy);
return is;
}

参见 Live On Coliru

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

typedef enum { UNSET = 0, RED = 1, GREEN = 2, BLUE = 3} Color;

enum vertex_one_t { vertex_one };
namespace boost { BOOST_INSTALL_PROPERTY(vertex, one); }

enum vertex_two_t { vertex_two };
namespace boost {
BOOST_INSTALL_PROPERTY(vertex, two);
}

enum vertex_three_t { vertex_three };
namespace boost {
BOOST_INSTALL_PROPERTY(vertex, three);
}

typedef boost::property< boost::vertex_index_t, int,
boost::property< vertex_one_t, Color,
boost::property< vertex_two_t, Color,
boost::property< vertex_three_t, Color > > > > DualVertexProperty;

inline static std::ostream& operator<<(std::ostream& os, Color color) {
return os << static_cast<int>(color);
}

inline static std::istream& operator>>(std::istream& is, Color& color) {
int dummy;
is >> dummy;
color = static_cast<Color>(dummy);
return is;
}

int main() {
using DualGraph = boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS, DualVertexProperty, boost::no_property>;

boost::dynamic_properties dpDual;
DualGraph g;
std::mt19937 rng { std::random_device{}() };
boost::generate_random_graph(g, 5, 5, rng);

boost::property_map<DualGraph, boost::vertex_index_t>::type vIndex = boost::get(boost::vertex_index, g);
boost::property_map<DualGraph, vertex_one_t>::type vOne = boost::get(vertex_one, g);

dpDual.property("node_id", vIndex);
dpDual.property("Color_1", vOne);

boost::write_graphviz_dp(std::cout, g, dpDual);
}

更复杂

您可以(尝试)使指针可流化,但您必须想办法让生命周期和所有权有意义。

奖励:

考虑捆绑属性。除非您有非常具体的要求,否则无需像您的代码那样继续使用陈旧的内部属性。

Live On Coliru (编译超时)

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

typedef enum { UNSET = 0, RED = 1, GREEN = 2, BLUE = 3} Color;

struct DualVertexProperty {
Color one, two, three;
};

inline static std::ostream& operator<<(std::ostream& os, Color color) { return os << static_cast<int>(color); }
inline static std::istream& operator>>(std::istream& is, Color& color) { int dummy; is >> dummy; color = static_cast<Color>(dummy); return is; }

int main() {
using DualGraph = boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS, DualVertexProperty, boost::no_property>;

boost::dynamic_properties dpDual;
DualGraph g;
std::mt19937 rng { std::random_device{}() };
boost::generate_random_graph(g, 5, 5, rng);

for (auto v: boost::make_iterator_range(boost::vertices(g)))
g[v] = DualVertexProperty { RED, GREEN, BLUE };

dpDual.property("node_id", boost::get(boost::vertex_index, g));
dpDual.property("Color_1", boost::get(&DualVertexProperty::one, g));

boost::write_graphviz_dp(std::cout, g, dpDual);
}

这在很多方面都简单多了!

关于c++ - boost C++ 图 write_graphviz_dp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37554177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com