gpt4 book ai didi

c++ - 如何访问 boost 图形库中的边属性

转载 作者:行者123 更新时间:2023-12-02 10:39:10 24 4
gpt4 key购买 nike

我正在尝试编写一个包装 boost 图形库的程序,以提供方便的用户界面。我对 boost (和堆栈溢出)非常陌生,但花了很多时间阅读
boost 文档。

我的问题是,每当我为我的边缘属性获取属性映射时,无论我使用的是捆绑属性还是内部属性,在调用 get() 时都会出现错误。边缘属性的函数,表示没有与给定参数匹配的函数原型(prototype)。为我的顶点属性做同样的事情时我没有困难。此外,boost 文档似乎也表明没有 get()边缘属性的函数。我认为问题可能出在我的构造函数中,该构造函数旨在从文本文件中读取图形信息:

BGraph::BGraph()                                                           // constructor
{
graph; // is this how you would initialize the graph?
//...input and initialization of vertices

//...input of edge information

auto e = add_edge(vertex1, vertex2, graph).first; // add the edge

// this is the internal property version
property_map<Graph, edge_weight_t>::type weightMap = get(edge_weight, graph);
// ^^^^^ error here saying there is no such member
weightMap[e] = inWeight;

//graph[e].weight = inWeight; this is the bundled version // give the edge its weight

}

}

这是包装类的头文件:
#include <boost/config.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_iterator.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/kruskal_min_spanning_tree.hpp>
#include <boost/graph/named_function_params.hpp>
#include <boost/graph/properties.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/graphviz.hpp>

#include <string>
#include <vector>
#include <iostream>

using namespace std;
using namespace boost;

class BGraph
{
public:

struct Edge_Properties // property bundle for edges
{
string name;
int weight = 1;
};

struct Vertex_Properties // property bundle for vertices
{
string name;
int distance;
int pred;
};

// class member functions...

typedef adjacency_list<vecS, vecS, bidirectionalS, // graph type
Vertex_Properties, property<edge_weight_t, int> > Graph;

/*typedef adjacency_list<vecS, vecS, bidirectionalS, // graph type
Vertex_Properties, Edge_Properties> Graph;*/ // this is the bundled version

typedef property_map<Graph, vertex_index_t>::type IdMap;
typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
private:
Graph graph; // the boost graph
}

我在 dijkstra_shortest_paths 的函数中有类似的问题:
dijkstra_shortest_paths(graph, findVertex(startVertex), 
predecessor_map(get(&Vertex_Properties::pred, graph))
.distance_map(get(&Vertex_Properties::distance, graph))
.weight_map(/*get(&Edge_Properties::weight, graph)*/ get(edge_weight, graph)));

get函数上的具体报错如下:

no instance of overloaded function "get" matches the argument list. argument types are: (boost:edge_weight_t, const BGraph::graph)



我觉得有一些过于简单的解决方案,但我一直无法找到它。我正在使用 MS Visual Studio 2017 和 boost 版本 boost_1_67_0。我认为这个问题可能与 Visual Studio 特别有关,因为与我的代码几乎相同的代码似乎也适用于其他人。谢谢你的帮助。

最佳答案

看来您的代码应该适用于内部属性。

当您使用捆绑属性时,您需要指定 PropertyTag对于 property_map作为指向数据成员的指针,在您的情况下指向 weight Edge_properties 的成员.

    property_map<Graph,int Edge_Properties::*>::type weightMap = 
get(&Edge_Properties::weight, graph);
//^^^^^
auto e = add_edge(1, 2, graph).first;
weightMap[e] = 10;
graph[e].weight = 20;

Here是从捆绑属性获取属性映射的示例。您可以轻松地使它们适应您的需求。

关于c++ - 如何访问 boost 图形库中的边属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53550797/

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