c++ - 不同 boost 图的相同权重

标签 c++ boost boost-graph

我才发现,我还没有明白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”。每个图都有它的权重属性图,不是吗?我的错误在哪里?

最佳答案

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

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

应该是:

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

因此,您将第一个图形的内容打印两次,而不是先打印再打印第二个。

关于c++ - 不同 boost 图的相同权重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30809645/

相关文章:

c++ - Windows api 中是否有 O(1) 方式来连接 2 个文件?

c++ - 使用 %。在 printf 中

c++ - 在 C++ 中处理 boost 线程竞争条件

c++ - modify() 在迭代期间 boost 多索引键

c++ - Boost-graph:当我的图形使用listS作为VertexList时,如何调用depth-first-search()?

c++ - BGL : How do I store edge_descriptors and vertex_descriptors efficiently?

c++ - 比特流解析和字节顺序

java - 如何处理无法将时间存储在 int 中?

c++ - 具有普通 STL 接口(interface)的最大数组,类似于 boost::array

c++ - 我在 MyGraph 属性顶点名称和边权重方面遇到问题