c++ - 创建没有图形的边缘描述符

标签 c++ boost

在我的项目中,我使用一个文件来存储所有边;另一个存储边缘概率。我想对图形使用 Boost 库,对概率使用 unordered_map。我有以下代码。

typedef boost::adjacency_list <boost::vecS, boost::vecS, boost::bidirectionalS> DiGraph;
typedef boost::graph_traits<SubGraph>::vertex_descriptor vertex_t;
typedef boost::graph_traits<SubGraph>::edge_descriptor edge_t;

unordered_map<edge_t, double> P;

ifstream infile(prob_filename);
double p;
int u, v;
while (infile >> u >> v >> p) {
    P[make_pair(u, v)] = p;
}

不过,我不需要做一对作为键,而是一个边描述符edge_t。如何使用给定的两个值 uv 创建边缘描述符。

最佳答案

From boost graph concepts

DirectedGraph digraph(V);
  {
    boost::graph_traits<DirectedGraph>::vertex_descriptor u, v;
    u = vertex(0, digraph); // read these instead 0,1 from from your file
    v = vertex(1, digraph);
    // populate graph
    add_edge(digraph, u, v, Weight(1.2)); // read weight (prob) instead from from your file

    boost::graph_traits<DirectedGraph>::edge_descriptor e;
    bool found;
    boost::tie(e, found) = edge(u, v, digraph);

    //use property map
    property_map<DirectedGraph, edge_weight_t>::type
      weight = get(edge_weight, digraph);

    cout << "weight[(u,v)] = " << get(weight, e) << endl;

  }

关于c++ - 创建没有图形的边缘描述符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34070195/

相关文章:

c++ - 检查函数的概念不适用于仅可移动的参数

c++ - 字符数组到单个整数

C++ 模板 sfinae 错误

c++ - vector (反)序列化与 Boost.serialization 的向后兼容性

c++ - 我什么时候必须使用 boost::asio:strand

c++ - 我应该在 boost 几何函数中使用什么

c++ - 代码无限循环,我不明白为什么 C++

c++ - 字节数组转 UTF8 CString

c++ - 在二叉搜索树中查找元素仅在 true 时有效

c++ - 如何将重新绑定(bind)与自定义分配器和自定义列表一起使用