下面是一個簡單的例子,我可以想出(其也使用在輸出屬性):
Live On Coliru
#include <boost/graph/grid_graph.hpp>
#include <boost/graph/properties.hpp>
#include <boost/graph/graphviz.hpp>
#include <iostream>
struct sampleVertex {
int row;
int col;
bool occupied;
friend std::ostream& operator<<(std::ostream& os, sampleVertex& sv) {
return os << "{" << sv.row << "," << sv.col << "," << sv.occupied << "}";
}
friend std::istream& operator>>(std::istream& is, sampleVertex& sv) {
return is >> sv.row >> sv.col >> sv.occupied;
}
};
int main() {
boost::array<int, 2> lengths = { { 3, 2 } };
using Graph = boost::grid_graph<2, int>;
using Traits = boost::graph_traits<Graph>;
using IdMap = boost::property_map<Graph, boost::vertex_index_t>::const_type;
Graph gridD(lengths);
IdMap indexMap(get(boost::vertex_index, gridD));
// properties
boost::vector_property_map<sampleVertex, IdMap> props(num_vertices(gridD), indexMap);
// initialize
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 2; ++j)
put(props, Traits::vertex_descriptor {{i, j}}, sampleVertex{i,j,false});
// print a property
boost::dynamic_properties dp;
dp.property("node_id", props);
boost::write_graphviz_dp(std::cout, gridD, dp);
}
輸出:
digraph G {
"{0,0,0}";
"{1,0,0}";
"{2,0,0}";
"{0,1,0}";
"{1,1,0}";
"{2,1,0}";
"{0,0,0}"->"{1,0,0}" ;
"{1,0,0}"->"{2,0,0}" ;
"{0,1,0}"->"{1,1,0}" ;
"{1,1,0}"->"{2,1,0}" ;
"{1,0,0}"->"{0,0,0}" ;
"{2,0,0}"->"{1,0,0}" ;
"{1,1,0}"->"{0,1,0}" ;
"{2,1,0}"->"{1,1,0}" ;
"{0,0,0}"->"{0,1,0}" ;
"{1,0,0}"->"{1,1,0}" ;
"{2,0,0}"->"{2,1,0}" ;
"{0,1,0}"->"{0,0,0}" ;
"{1,1,0}"->"{1,0,0}" ;
"{2,1,0}"->"{2,0,0}" ;
}
您好sehe ,我有要求將動態屬性寫入文件。我確實改變了流,但那給了我錯誤。任何想法我應該改變,將動態屬性寫入DOT文件。 – soupso
我的代碼已經做到了。 Sooo ...也許你想展示你想要做什麼(也許是一個新問題)。 – sehe