一个boost :: adjaceny_list我能够得到并设置图形的名称。可悲的是,我无法将其保存并加载到文件中。我可以保存和加载顶点和边缘,但图形属性的工作方式不同。
在下面的完整测试编译代码中,我展示了:
首先是基础工作:
这些功能接近于简单:
#include <boost/graph/adjacency_list.hpp>
#include <string>
boost::adjacency_list<
boost::vecS,
boost::vecS,
boost::directedS,
boost::no_property,
boost::no_property,
boost::property<boost::graph_name_t,std::string>
>
create_empty_directed_graph_with_graph_name() noexcept
{
return {};
}
template <typename graph>
void set_graph_name(const std::string& name, graph& g) noexcept
{
get_property(g, boost::graph_name) = name;
}
template <typename graph>
std::string get_graph_name(const graph& g) noexcept
{
return get_property(g, boost::graph_name
);
}
我想去上班的考试:
int main()
{
const std::string dot_filename{"test.dot"};
auto g = create_empty_directed_graph_with_graph_name();
set_graph_name("TestGraph", g);
save_graph_with_graph_name_to_dot(g, dot_filename);
const auto h = load_directed_graph_with_graph_name_from_dot(dot_filename);
assert(get_graph_name(g) == get_graph_name(h));
}
步骤是:(1)设置图形名称(2)保存(3)加载图形(4)检查是否具有相同名称
我所提出的代码是保存。我用这样的名字保存图表:
template <typename graph>
void save_graph_with_graph_name_to_dot(
const graph& g, const std::string& filename
)
{
std::ofstream f(filename);
boost::write_graphviz(
f,
g,
boost::default_writer(),
boost::default_writer(),
[g](std::ostream& os) {
os << "name=\""
<< get_graph_name(g)
<< "\";\n";
}
);
}
我不确定这是否可行。它生成以下.dot文件:
digraph G {
name="TestGraph";
}
我想知道它是不是应该说&#39; digraph TestGraph&#39;相反,它被接受为有效的.dot文件。
加载此文件是我被困在的地方。我甚至不知道如何编译它。我知道如何(保存和)加载任何类型的边和顶点(见a Boost.Graph tutorial),但在这里我迷路了。这是我的尝试:
boost::adjacency_list<
boost::vecS,
boost::vecS,
boost::directedS,
boost::no_property,
boost::no_property,
boost::property<boost::graph_name_t, std::string>
>
load_directed_graph_with_graph_name_from_dot(
const std::string& dot_filename
)
{
std::ifstream f(dot_filename.c_str());
auto g = create_empty_directed_graph_with_graph_name();
//#define TODO_KNOW_HOW_TO_LOAD_A_GRAPH_ITS_NAME
#ifdef TODO_KNOW_HOW_TO_LOAD_A_GRAPH_ITS_NAME
boost::dynamic_properties p;
p.property("name",get_property(g,boost::graph_name)); //Something like this?
#else
boost::dynamic_properties p(
boost::ignore_other_properties
);
#endif
boost::read_graphviz(f,g,p);
return g;
}
我希望有人知道如何保存和加载带有名称的图表。它不应该太难了,不应该吗?
以下是完整的完整列表:
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/graph/properties.hpp>
#include <cassert>
#include <fstream>
#include <string>
boost::adjacency_list<
boost::vecS,
boost::vecS,
boost::directedS,
boost::no_property,
boost::no_property,
boost::property<boost::graph_name_t,std::string>
>
create_empty_directed_graph_with_graph_name() noexcept
{
return {};
}
template <typename graph>
void set_graph_name(const std::string& name, graph& g) noexcept
{
get_property(g, boost::graph_name) = name;
}
template <typename graph>
std::string get_graph_name(const graph& g) noexcept
{
return get_property(g, boost::graph_name
);
}
template <typename graph>
void save_graph_with_graph_name_to_dot(
const graph& g, const std::string& filename
)
{
std::ofstream f(filename);
boost::write_graphviz(
f,
g,
boost::default_writer(),
boost::default_writer(),
//Unsure if this results in a graph that can be loaded correctly from a .dot file
[g](std::ostream& os) {
os << "name=\""
<< get_graph_name(g)
<< "\";\n";
}
);
}
boost::adjacency_list<
boost::vecS,
boost::vecS,
boost::directedS,
boost::no_property,
boost::no_property,
boost::property<boost::graph_name_t, std::string>
>
load_directed_graph_with_graph_name_from_dot(
const std::string& dot_filename
)
{
std::ifstream f(dot_filename.c_str());
auto g = create_empty_directed_graph_with_graph_name();
//#define TODO_KNOW_HOW_TO_LOAD_A_GRAPH_ITS_NAME
#ifdef TODO_KNOW_HOW_TO_LOAD_A_GRAPH_ITS_NAME
boost::dynamic_properties p;
p.property("name",get_property(g,boost::graph_name)); //Something like this?
#else
boost::dynamic_properties p(
boost::ignore_other_properties
);
#endif
boost::read_graphviz(f,g,p);
return g;
}
int main()
{
const std::string dot_filename{"test.dot"};
auto g = create_empty_directed_graph_with_graph_name();
set_graph_name("TestGraph", g);
save_graph_with_graph_name_to_dot(g, dot_filename);
const auto h = load_directed_graph_with_graph_name_from_dot(dot_filename);
assert(get_graph_name(g) == get_graph_name(h));
}
答案 0 :(得分:0)
cv_and_he:谢谢!我确实看到了这个页面,但很难读到它!
答案:代码的加载部分需要更改为:
boost::adjacency_list<
boost::vecS,
boost::vecS,
boost::directedS,
boost::no_property,
boost::no_property,
boost::property<boost::graph_name_t, std::string>
>
load_directed_graph_with_graph_name_from_dot(
const std::string& dot_filename
)
{
using graph = boost::adjacency_list<
boost::vecS,
boost::vecS,
boost::directedS,
boost::no_property,
boost::no_property,
boost::property<boost::graph_name_t, std::string>
>;
std::ifstream f(dot_filename.c_str());
auto g = create_empty_directed_graph_with_graph_name();
boost::ref_property_map<graph*,std::string>
graph_name(get_property(g,boost::graph_name)
);
boost::dynamic_properties dp;
dp.property("name",graph_name);
boost::read_graphviz(f,g,dp);
return g;
}