gpt4 book ai didi

c++ - 如何使用boost make_label_writer 来写边缘属性?

转载 作者:搜寻专家 更新时间:2023-10-31 01:28:50 25 4
gpt4 key购买 nike

我有一个简单的图,我成功地用顶点写入属性,但是当我使用 make_label_writer 将属性写入边时,编译器总是提示。有人可以帮忙吗?

我的代码如下:

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() 将调用模板,这非常好:

  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])时,出现错误。

最佳答案

默认的顶点索引是整数,这就是为什么你可以使用第一个顶点名称的地址作为隐含的关联属性映射。

边缘描述符是一个不同的野兽,需要你要么

  • 创建一个显式迭代器属性映射(使用额外的索引属性映射将边描述符映射到 name1 vector 中的整数索引)
  • 或使用 Associative PropertyMap 概念的模型。

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

还请考虑使用 Bundled Properties 让您的特性生活变得更简单.

关联属性映射

Live On 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)));
}

打印

改为捆绑属性

Live On 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)));
}

打印相同

关于c++ - 如何使用boost make_label_writer 来写边缘属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51138794/

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