c++ - bgl 中的 edge_descriptor 映射

标签 c++ boost graph map boost-graph

我是 BGL 的新手,我在制作自己的属性映射时遇到问题,键是 edge_property

你能告诉我下面的代码打印不出来我做错了什么吗: (0,1) == (0,1) ? 1个 但 (0,1) == (0,1) ? 0

这是代码

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

using namespace std;


class A {};
class B {};

typedef boost::adjacency_list<boost::listS, boost::vecS, boost::bidirectionalS,
                                    A, B > Graph;

typedef boost::graph_traits<Graph>::vertex_descriptor vertex_descriptor;
typedef boost::graph_traits<Graph>::edge_descriptor edge_descriptor;
typedef boost::graph_traits<Graph>::edge_iterator edge_iterator;
typedef boost::graph_traits<Graph>::in_edge_iterator in_edge_iterator;
typedef boost::graph_traits<Graph>::out_edge_iterator out_edge_iterator;

map<edge_descriptor, int>  fun(Graph g){

    map<edge_descriptor, int> m;
    m[*(edges(g).first)] = 5;
    return m;
}


int main(){
    Graph g;

    vertex_descriptor a = add_vertex(g);
    vertex_descriptor b = add_vertex(g);

    add_edge(a, b, g);

    map<edge_descriptor, int> m = fun(g);

    edge_iterator ei, ei_end;
    for(tie(ei, ei_end) = edges(g) ; ei !=ei_end ; ++ei){
        cout << m.begin()->first << " == " << *ei << " ? " << (m.begin()->first == *ei) << endl;
    }
}

非常感谢!

编辑: 也许有比通过 edge_property 值映射边缘更好的方法?

最佳答案

您的边描述符有 3 个成员:源节点、目标节点和指向 B 属性的指针。在你的fun你复制了你的图形,新图形中复制的边指向不同的 B。如果你声明你的 fun作为map<edge_descriptor, int> fun(const Graph& g) (您可能应该这样做,因为更大图表的拷贝可能很昂贵)您获得了预期的结果。

关于c++ - bgl 中的 edge_descriptor 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13488994/

相关文章:

python - Matplotlib 按 Y 值绘制散点图颜色

c++ - 带有可选参数的构造函数

c++ - SDL_Init 失败并显示 'SDL_Error: Failed to connect to the Mir Server'

c++ - nuget 引用 c++ header

c++ - 为什么不推荐使用 boost::optional::is_initialized() ?

r - 如何在 R 中绘制 SVM 的分类图

c++ - 在 DirectShow 中保持纵横比? (窗口)C++

c++ - C++14 中 decltype(auto) 的转换函数

c++ - 使用 C++ 在 Windows 中实现 < 1ms 的延迟

java - 在 Java 中从 char[][] 打印图形