[英]How to use boost make_label_writer to write edge properties?
I have a simple graph, I suceeded writing properties with the vertex, but when I use make_label_writer
to write properties to the edges, the complier always complains.我有一个简单的图形,我成功地用顶点写入属性,但是当我使用make_label_writer
将属性写入边缘时,编译器总是抱怨。 Could someone help with it?有人可以帮忙吗?
My code is as following:我的代码如下:
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]));
}
The write_graphviz()
will ofc called the template, which is perfectly fine : 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);
}
So the problem is now: when I only write the vertex properties using make_label_writer(&name[0]]])
, the code runs perfectly.所以现在的问题是:当我只使用make_label_writer(&name[0]]])
编写顶点属性时,代码运行完美。 But when I add make_label_writer(&name1[0])
, there is error.但是当我添加make_label_writer(&name1[0])
,出现错误。
The default vertex index is integral, which is why you can use the address of the first vertex name as implied associative property map.默认顶点索引是整数,这就是为什么您可以使用第一个顶点名称的地址作为隐含关联属性映射的原因。
The edge descriptor is a different beast and requires you to either边缘描述符是一个不同的野兽,需要你
name1
vector)创建一个显式迭代器属性映射(使用额外的索引属性映射从边缘描述符映射到整数索引到name1
向量) In this case you should property do the later using a std::map<edge_descriptor, std::string>
.在这种情况下,您应该使用std::map<edge_descriptor, std::string>
属性操作。
Please also consider making your life with properties a lot simpler by using Bundled Properties .还请考虑使用Bundled Properties使您的属性生活更简单。
#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)));
}
Prints印刷
#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)));
}
Prints the same打印相同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.