简体   繁体   English

如何访问Boost图形库中的边缘属性

[英]How do you access edge properties in the boost graph library

I am trying to write a program that wraps the boost graph library so as to provide a convenient user interface. 我试图编写一个包装升压图形库的程序,以便提供一个方便的用户界面。 I am very new to boost (and stack overflow) but have spent quite a bit of time reading the boost documentation. 我对提升(和堆栈溢出)非常陌生,但是花了很多时间阅读提升文档。

My problem is that whenever I go to get a property map for my edge properties, whether I am using bundled properties or internal properties, I get an error when ever call the get() function for an edge property saying that there is no function prototype that matches the given arguments. 我的问题是,无论何时我要获取边缘属性的属性映射,无论是使用捆绑属性还是内部属性,调用边缘属性的get()函数都说没有函数原型时会出错与给定参数匹配。 I have no difficulty when doing the same thing for my vertex properties. 对顶点属性进行相同操作时,我没有任何困难。 Moreover, the boost docs seem to also indicate that there is no get() function for edge properties. 此外,boost文档似乎还表明边缘属性没有get()函数。 I think the problem may be in my constructor, which is designed to read graph information from a text file: 我认为问题可能出在我的构造函数中,该构造函数旨在从文本文件中读取图形信息:

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

}

} }

Here is my header file for the wrapper class: 这是包装类的头文件:

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

I have similar problems in my function for dijkstra_shortest_paths : 我的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)));

The specific error on the get function is as follows: get函数的特定错误如下:

no instance of overloaded function "get" matches the argument list. 没有重载函数“ get”的实例与参数列表匹配。 argument types are: (boost:edge_weight_t, const BGraph::graph) 参数类型为: (boost:edge_weight_t, const BGraph::graph)

I feel like there is some overly simple solution, but I just have not been able to find it. 我觉得这里有一些过于简单的解决方案,但我只是找不到。 I am using MS Visual Studio 2017 and boost version boost_1_67_0. 我正在使用MS Visual Studio 2017和增强版本boost_1_67_0。 I think the problem may have something to do with Visual Studio in particular because code almost identical to mine seems to work for other people. 我认为问题可能与Visual Studio有关,尤其是因为与我的代码几乎相同的代码似乎对其他人也有效。 Thanks for your help. 谢谢你的帮助。

It seems that your code should work for internal properties. 看来您的代码应适用于内部属性。

When you use bundled properties you need to specify PropertyTag for property_map as pointer to data member, in your case pointer to weight member of Edge_properties . 使用捆绑属性时,需要为property_map指定PropertyTag作为指向数据成员的指针,在这种情况下,指向Edge_properties 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 are examples of getting property map from bundled properties. 是从捆绑的属性获取属性映射的示例。 You can easily adapt them to your needs. 您可以轻松地使它们适应您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM