无法添加命名顶点(根据教程)
Cannot add named vertex (according to tutorial)
我现在正在学习 BGL,我找到了一个 tutorial。一切正常,直到我达到 add_named_vertex 的功能。这是我的一段代码,它没有像我(和教程)期望的那样工作:
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <type_traits>
#include <iostream>
#include <sstream>
#include <string>
boost::adjacency_list<
boost::vecS,
boost::vecS,
boost::directedS,
boost::property<boost::vertex_name_t, std::string>
>
create_empty_graph() { return {}; }
template<typename graph, typename name_type>
typename boost::graph_traits<graph>::vertex_descriptor
add_named_vertex(const name_type& vertex_name, graph& g) noexcept {
const auto vd = boost::add_vertex(g);
auto vertex_name_map = get(boost::vertex_name, g);
put(vertex_name_map, vd, vertex_name);
return vd;
}
int main()
{
auto g = create_empty_graph();
const auto va = add_named_vertex("One", g);
const auto vb = add_named_vertex("Two", g);
boost::add_edge(va,vb, g);
std::stringstream f;
boost::write_graphviz(f, g);
std::cout << f.str() << std::endl;
return 0;
}
我预计:
digraph G {
0[label=One];
1[label=Two];
0->1;
}
但这是我得到的:
digraph G {
0;
1;
0->1;
}
如您所见,此代码的输出中没有标签。你能告诉我,我错过了什么吗?这是预期的行为吗?
尝试了 clang++ 和 gcc 以及 Boost 版本范围 (1.69 - 1.71)。
是的,这是预期的行为。要打印标签,请添加 属性 作者:
auto vlw = boost::make_label_writer(boost::get(boost::vertex_name, g));
boost::write_graphviz(f, g, vlw);
或者,如我所愿,使用write_graphviz_dp
来使用dynamic_properties
:
boost::dynamic_properties dp;
dp.property("node_id", boost::get(boost::vertex_index, g));
dp.property("label", boost::get(boost::vertex_name, g));
boost::write_graphviz_dp(f, g, dp);
这似乎需要更多工作,但它具有许多 vertex/edge 属性,既简单又灵活。您可以 search my answers 找到很好的例子。
以上解决方案均打印
digraph G {
0[label=One];
1[label=Two];
0->1 ;
}
奖金
您不需要 add_named_vertex
函数。您可以直接使用 boost::add_vertex
:
初始化 属性
const auto va = add_vertex({"One"}, g);
const auto vb = add_vertex({"Two"}, g);
add_edge(va, vb, g);
我现在正在学习 BGL,我找到了一个 tutorial。一切正常,直到我达到 add_named_vertex 的功能。这是我的一段代码,它没有像我(和教程)期望的那样工作:
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <type_traits>
#include <iostream>
#include <sstream>
#include <string>
boost::adjacency_list<
boost::vecS,
boost::vecS,
boost::directedS,
boost::property<boost::vertex_name_t, std::string>
>
create_empty_graph() { return {}; }
template<typename graph, typename name_type>
typename boost::graph_traits<graph>::vertex_descriptor
add_named_vertex(const name_type& vertex_name, graph& g) noexcept {
const auto vd = boost::add_vertex(g);
auto vertex_name_map = get(boost::vertex_name, g);
put(vertex_name_map, vd, vertex_name);
return vd;
}
int main()
{
auto g = create_empty_graph();
const auto va = add_named_vertex("One", g);
const auto vb = add_named_vertex("Two", g);
boost::add_edge(va,vb, g);
std::stringstream f;
boost::write_graphviz(f, g);
std::cout << f.str() << std::endl;
return 0;
}
我预计:
digraph G {
0[label=One];
1[label=Two];
0->1;
}
但这是我得到的:
digraph G {
0;
1;
0->1;
}
如您所见,此代码的输出中没有标签。你能告诉我,我错过了什么吗?这是预期的行为吗? 尝试了 clang++ 和 gcc 以及 Boost 版本范围 (1.69 - 1.71)。
是的,这是预期的行为。要打印标签,请添加 属性 作者:
auto vlw = boost::make_label_writer(boost::get(boost::vertex_name, g));
boost::write_graphviz(f, g, vlw);
或者,如我所愿,使用write_graphviz_dp
来使用dynamic_properties
:
boost::dynamic_properties dp;
dp.property("node_id", boost::get(boost::vertex_index, g));
dp.property("label", boost::get(boost::vertex_name, g));
boost::write_graphviz_dp(f, g, dp);
这似乎需要更多工作,但它具有许多 vertex/edge 属性,既简单又灵活。您可以 search my answers 找到很好的例子。
以上解决方案均打印
digraph G {
0[label=One];
1[label=Two];
0->1 ;
}
奖金
您不需要 add_named_vertex
函数。您可以直接使用 boost::add_vertex
:
const auto va = add_vertex({"One"}, g);
const auto vb = add_vertex({"Two"}, g);
add_edge(va, vb, g);