使用boost图库将boost动态属性写入文件
Writing boost dynamic properties to a file using Boost Graph Library
我已经在这里问了一个关于使用Boost图库和将图写入文件的问题。由于我的需求发生了变化,我需要将动态图形属性写入DOT文件。经过一番查找,我设法想出了一些代码,但它不起作用。以下是我到目前为止所做的:
Map类使用Cell类作为顶点,Cell类使用单独的CellProperty类来设置和获取所有Cell属性。
最后是Map类,我在其中构建了图形,并尝试将图形写入DOT文件。
Map.h
class Map {
public:
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, Cell> Graph;
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
explicit Map(std::string pGraphFilePath);
virtual ~Map();
void LoadGraph();
private:
Graph mGraph;
std::vector<std::vector<Vertex>> mGrid;
};
Map.cpp
const unsigned int RowNum = 3;
const unsigned int ColumnNum = 4;
Map::Map(std::string pGraphFilePath) : mGraph(), mGrid() {}
Map::~Map() {}
void Map::LoadGraph() {
int dummyID = 1;
for (unsigned int row = 0; row < RowNum; row++) {
mGrid.resize(RowNum);
for (unsigned int col = 0; col < ColumnNum; col++) {
mGrid[row].resize(ColumnNum);
Vertex vID = boost::add_vertex(mGraph);
mGraph[vID].SetProperty<unsigned int>("ID", dummyID);
mGraph[vID].SetProperty<bool>("Navigable", true);
mGrid[row][col] = vID;
dummyID++;
// add the edges for the contained cells in the grid
if (col > 0) { boost::add_edge(mGrid[row][col - 1], mGrid[row][col], mGraph); }
if (row > 0) { boost::add_edge(mGrid[row - 1][col], mGrid[row][col], mGraph); }
}
}
// write cell properties
boost::dynamic_properties propertiesOutPut;
propertiesOutPut.property("ID", boost::get(boost::vertex_index, mGraph));
// As Navigable is an external property, it need to be mapped with the internal graph property
// the lines below are the update after I got the answers and link for my query
// cell.GetProperty() is a templated method the takes a default parameter, thus passing "false" bool parameter which returns the "Navigable" cell property
auto valueNavigable = boost::make_transform_value_property_map([](Cell &cell) { return cell.GetProperty<bool>("Navigable", false); }, boost::get(boost::vertex_bundle, mGraph));
propertiesOutPut.property("Navigable", valueNavigable);
std::ofstream fout("MyGraph.dot");
boost::write_graphviz_dp(fout, mGraph, propertiesOutPut, std::string("ID"));
}
我遇到的问题是boost::get()的propertiesOutPut.property()方法。我想不出boost::get()的正确参数。请帮帮我。谢谢
您可以在包含顶点属性结构的属性映射上使用transform_value_property_map
。(你没有展示)。
我有很多答案显示了如何做到这一点,尽管这些都使用了内部属性,但没有太大区别,因为无论属性映射是内部还是外部,都可以以相同的方式转换anu属性映射(这就是属性映射的全部目的:解耦访问属性的方式)。
最相关:
-
GraphViz 的Boost Graph捆绑包输出的流式操作符过载
-
手动着色boost';s图
其他:
- 查看以下搜索结果:https://stackoverflow.com/search?tab=votes&q=用户%3a85371%20make_transform_value_property_map
相关文章:
- boost::文件系统::recursive_directory_iterator多线程安全
- 使用Boost文件系统C++将具有特定扩展名的文件的名称保存在特定文件夹中
- 将 boost::文件系统::p ath 转换为 char?
- 程序使用boost::文件系统链接clang,但不使用gcc
- Boost 文件在 /usr/local/include 和 /usr/include 中有所不同
- 为什么 boost 文件系统和 libpq-fe 标头不会在同一个文件中编译
- boost::文件系统递归获取每个文件的大小
- boost::文件系统::create_directory抛出了一个提升::文件系统::filesystem_error
- 针对 QNX 交叉编译后,boost 文件系统 (1.63) 失败并出现未定义的引用错误
- ctime 包含 boost::文件系统时出错
- 获取“boost::文件系统::p ath”字符指针时出现问题
- 添加 boost 文件系统.hpp 无法编译
- Boost文件系统路径中的分割故障
- Boost文件系统存在访问违规
- C++17 相当于 boost::文件系统::unique_path()
- C++ boost::文件系统如何检测路径是文件还是目录
- Cygwin目录迭代器中的Boost文件系统
- 使用boost ::文件系统库中的输出中的奇怪字符
- 如何确保 boost::文件系统::remove 不会尝试删除由其他进程使用的文件
- 尽管使用了boost_NO_CXX11_SCOPED_ENUMS,但boost::文件系统出现未定义的引用错误