我使用Boost Graph库来读取GraphML文件。我想要做的是使用图形管理的Boost功能来创建我自己动态分配的对象结构,以便我可以在其上运行我的自定义算法。
struct VertexProperties {
std::string vertex_name;
bool defect;
bool node_logic;
custom_node * node;
};
typedef adjacency_list<vecS, vecS, directedS, VertexProperties> DirectedGraph;
但问题是,在使用Deep First Search自定义访问者时,我似乎无法为 custom_node 指针分配内存。
node = new custom_node(g[v].vertex_name, 0, standby, normal);
当我得到&#34;只读&#34;编译错误。
如果有一种方法可以将图形映射到其他可以使用动态分配来重新创建图形结构的东西,那么我是在徘徊?
修剪主要:
#include <iostream>
#include <fstream>
#include <boost/graph/graphml.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include "node.h"
using namespace boost;
struct VertexProperties {
std::string vertex_name;
bool node_logic;
custom_node * node;
};
typedef adjacency_list<vecS, vecS, directedS, VertexProperties> DirectedGraph;
typedef graph_traits<DirectedGraph>::vertex_descriptor custom_vertex;
typedef graph_traits<DirectedGraph>::edge_descriptor custom_edge;
class custom_dfs_visitor : public default_dfs_visitor {
public:
void discover_vertex(custom_vertex v, const DirectedGraph& g) const
{
// Looking for adjacent vertices
DirectedGraph::adjacency_iterator neighbourIt, neighbourEnd;
if(true == g[v].node_logic)
{
g[v].node = new custom_node(g[v].vertex_name, 0, standby, normal);
}
std::cout << g[v].vertex_name << " is connected with ";
tie(neighbourIt, neighbourEnd) = adjacent_vertices(v, g);
for (; neighbourIt != neighbourEnd; ++neighbourIt) {
std::cout << g[*neighbourIt].vertex_name << " ";
}
std::cout << std::endl;
}
void examine_edge(custom_edge e, const DirectedGraph& g) const
{
std::cout << "Examining edges : " << g[e.m_source].vertex_name << " >> "
<< g[e.m_target].vertex_name << std::endl;
}
};
int main(int argc, char* argv[])
{
int i;
bool is_config = false;
DirectedGraph g;
int verbose;
std::ifstream infile;
dynamic_properties dp(ignore_other_properties);
custom_dfs_visitor vis;
dp.property("node_name", boost::get(&VertexProperties::vertex_name, g));
dp.property("node_logic", boost::get(&VertexProperties::node_logic, g));
/* Argument check */
if (argc <= 1 || (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'h')) {
usage();
return 0;
}
/* Parse command line options */
for (i = 1; (i + 1 < argc) && (argv[i][0] == '-'); i++) {
switch (argv[i][1]) {
case 'v': /* verbose */
verbose = 10;
break;
case 'c': /* read *.ini configuration file */
// d_printf(D_INFO, "parsing '%s'... \n", argv[++i]);
infile.open(argv[++i], std::ifstream::in);
if (!infile.is_open()) {
std::cout << "Loading file '" << argv[i] << "'failed" << std::endl;
throw "Could not load file";
}
else {
boost::read_graphml(infile, g, dp);
is_config = true;
}
break;
case 's': {
is_defect = true;
std::string temp_defect(argv[++i]);
defect = temp_defect;
std::cout << defect;
break;
}
default: /* something's wrong */
usage();
break;
}
}
if (true == is_config) {
depth_first_search(g, boost::visitor(vis));
}
return 0;
}
感谢您的回答
答案 0 :(得分:2)
好的,depht_first_visit必须适用于常量图。但是,显然不能修改常量图。
因此,您希望告诉访问者对图表的非常量引用,以便您可以通过它进行修改。
我建议进行以下微小改动:
<强> Live On Coliru 强>
#include <iostream>
#include <fstream>
#include <boost/graph/graphml.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
//#include "cgfnode.h"
enum S{standby};
enum M{normal};
struct custom_node{
custom_node(std::string, int, S, M){}
};
void usage(){}
using namespace boost;
struct VertexProperties {
std::string vertex_name;
bool node_logic;
std::unique_ptr<custom_node> node;
};
typedef adjacency_list<vecS, vecS, directedS, VertexProperties> DirectedGraph;
typedef graph_traits<DirectedGraph>::vertex_descriptor custom_vertex;
typedef graph_traits<DirectedGraph>::edge_descriptor custom_edge;
struct custom_dfs_visitor : default_dfs_visitor {
custom_dfs_visitor(DirectedGraph& g) : _mutable_graph(&g) {}
void discover_vertex(custom_vertex v, DirectedGraph const& g) const {
assert(&g == _mutable_graph);
return discover_vertex(v, *_mutable_graph);
}
void discover_vertex(custom_vertex v, DirectedGraph& g) const
{
// Looking for adjacent vertices
DirectedGraph::adjacency_iterator neighbourIt, neighbourEnd;
if(g[v].node_logic) {
g[v].node.reset(new custom_node(g[v].vertex_name, 0, standby, normal));
}
std::cout << g[v].vertex_name << " is connected with ";
tie(neighbourIt, neighbourEnd) = adjacent_vertices(v, g);
for (; neighbourIt != neighbourEnd; ++neighbourIt) {
std::cout << g[*neighbourIt].vertex_name << " ";
}
std::cout << "\n";
}
void examine_edge(custom_edge e, const DirectedGraph& g) const {
std::cout << "Examining edges : " << g[e.m_source].vertex_name << " >> " << g[e.m_target].vertex_name << "\n";
}
private:
DirectedGraph* _mutable_graph;
};
int main() {
DirectedGraph g;
{
dynamic_properties dp(ignore_other_properties);
dp.property("node_name", boost::get(&VertexProperties::vertex_name, g));
dp.property("node_logic", boost::get(&VertexProperties::node_logic, g));
std::ifstream infile("input.xml");
boost::read_graphml(infile, g, dp);
}
custom_dfs_visitor vis (g);
depth_first_search(g, boost::visitor(vis));
}