gpt4 book ai didi

c++ - 使用 Boost Graph 顶点属性进行动态分配

转载 作者:搜寻专家 更新时间:2023-10-31 02:09:02 24 4
gpt4 key购买 nike

我正在使用 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;

但问题是,在使用深度优先搜索自定义访问者时,我似乎无法为 custom_node 指针分配内存。

node = new custom_node(g[v].vertex_name, 0, standby, normal);

因为我收到“只读”编译错误。

我在想是否有办法将图形映射到其他可以使用动态分配重新创建图形结构的东西?

修剪主要 :

#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;
}

感谢您的回答

最佳答案

好的,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));
}

关于c++ - 使用 Boost Graph 顶点属性进行动态分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46928118/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com