繁体   English   中英

使用迭代器访问数据。 升压图

[英]Using iterators to access data. Boost graph

我是C ++和Boost图形库的新手。 我试图使用迭代器来访问已经存储在图形“ lattuce”中的信息,更具体地说,权重是两个特定节点之间的边缘。

然后,该数据将由A *算法使用(而不是Boost图中的一种)。 我不确定迭代器是否可以解决此问题,因此任何指导或批评都将不胜感激。

struct Point {//struct point with vertex properties
    int x, y;
    int parentx, parenty;
    double g;
    double h;
     friend std::ostream& operator<<(std::ostream& os, Point p) {
      return os << "[" << p.x << "," << p.y << "]";
    }
};

int main() {
//declarations
typedef property < edge_weight_t, double >Weight;
using std::vector;//?
using Graph = adjacency_list<setS, vecS, undirectedS, Point, Weight>;//graph includes our created point struct property<edge_weight_t
using vertex_descriptor = Graph::vertex_descriptor;
Graph lattuce;

//lattuce graph is created with weighted edges value 1 or 1,41 if diagonal. The functions used on a loop are:
//add_edge(nodes[p.x][p.y],nodes[neighbour.x][neighbour.y], Weight(1.0), lattuce);
//add_edge(nodes[p.x][p.y],nodes[neighbour.x][neighbour.y], Weight(1.4), lattuce);

如果需要有关生成图形的代码的更多信息,我将提供它。 谢谢

可以通过boost :: property_map获得有向图和无向图中的链接边缘权重:

boost::property_map<UndirectedGraph, boost::edge_weight_t>::type EdgeWeightMap = get(boost::edge_weight_t(), g);

下面给出的示例实现示例是,首先构建以下简单图形(特别是没有循环的树):

在此处输入图片说明

...然后使用boost :: property_map获得每个边缘的权重,并打印出来:

#include <iostream>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>

typedef boost::property<boost::edge_weight_t, double> EdgeWeight;
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, boost::no_property, EdgeWeight> UndirectedGraph;
typedef boost::graph_traits<UndirectedGraph>::edge_iterator edge_iterator;

int main(int, char*[])
{
    // 1. Undirected graph - print out the edge weights
    UndirectedGraph g;

    boost::add_edge(0, 1, 8, g);
    boost::add_edge(0, 5, 2, g);
    boost::add_edge(5, 6, 1, g);
    boost::add_edge(4, 5, 5, g);
    boost::add_edge(3, 5, 7, g);

    boost::property_map<UndirectedGraph, boost::edge_weight_t>::type EdgeWeightMap = get(boost::edge_weight_t(), g);

    std::pair<edge_iterator, edge_iterator> edgePair;
    for (edgePair = edges(g); edgePair.first != edgePair.second; ++edgePair.first)
    {
        std::cout << *edgePair.first << " " << EdgeWeightMap[*edgePair.first] << std::endl;
    }   

    return 0;
}

提供以下控制台输出,将边缘显示为(开始,结束)节点对加上它们各自的权重:

在此处输入图片说明

暂无
暂无

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

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