簡體   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