【问题标题】:Same weights for different boost graphs不同提升图的权重相同
【发布时间】:2015-06-12 18:11:07
【问题描述】:

我刚刚意识到我还没有理解如何使用boost图形库。我有这个代码:

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

using namespace std;
using namespace boost;

typedef unsigned int WeightType;

typedef adjacency_list<listS, vecS, bidirectionalS,
        no_property, property<edge_weight_t, WeightType>> Graph;

typedef graph_traits<Graph>::vertex_descriptor Vertex;
typedef graph_traits<Graph>::edge_descriptor Edge;

typedef property_map<Graph, edge_weight_t>::type WeightMap;
typedef property_map<Graph, edge_weight_t>::const_type ConstWeightMap;

const WeightType infinity = numeric_limits<WeightType>::max();

int main() {
    Graph g(4);
    Graph g2(4);

    for (uint i = 0; i < 3; ++i) {
        add_edge(i, i+1, i, g);
        add_edge(i, i+1, i*10, g2);
    }

    WeightMap m = get(edge_weight, g);
    WeightMap m2 = get(edge_weight, g2);

    for (auto e : make_iterator_range(edges(g))) {
        cout << m[e] << endl;
    }
    cout << endl;
    for (auto e : make_iterator_range(edges(g))) {
        cout << m2[e] << endl;
    }

}

我希望输出如下:“0 1 2 , 0 10 20”。但输出是“0 1 2, 0 1 2”。每个图都有其权重属性图,不是吗?我的错在哪里?

【问题讨论】:

    标签: c++ boost boost-graph


    【解决方案1】:

    你在第二个 for 循环中打错了:

    for (auto e : make_iterator_range(edges(g))) {
    

    应该是:

    for (auto e : make_iterator_range(edges(g2))) {
    

    所以您将第一个图表的内容打印了两次,而不是第一次打印第二次。

    【讨论】:

    • 谢谢。我很愚蠢。那么,是否只有一个权重图在所有图实例之间共享?因为我预计 m2[e] 会返回一个默认值,因为 e 不属于 g2。
    • @DanieleFerone 不,您从第一张图中获得了值,因为您需要它们! WeighMap 只是获取它们恰好存在的包含属性edge_weight_t 的“代理”(否则它不会编译)。此外,它们的边缘描述符恰好是相同的
    猜你喜欢
    • 2013-06-13
    • 2012-04-24
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多