【问题标题】:How to properly use bundled properties in boost?如何在 boost 中正确使用捆绑的属性?
【发布时间】:2018-05-11 13:41:23
【问题描述】:

我正在尝试修改无向图中的特定顶点。本质上,我希望能够从 18 的索引 1 开始,并运行一个算法来从该顶点更改每个节点的字符串。我从点文件中读取图表。我正在查看boost's website 的捆绑属性,它看起来很简单。但是,当我尝试实现这一点时,我得到Attempt to put a value into a const property map: Aborted (core dumped) 错误。所以我找到了this solution,它看起来和我的一模一样。

这是引发错误的一个最小示例:

我从 test.dot 读取的文件:

graph G {
0 [isLeaf=1];
1 [isLeaf=0];
2 [isLeaf=1];
3 [isLeaf=1];
4 [isLeaf=0];
5 [isLeaf=1];
6 [isLeaf=0];
7 [isLeaf=1];
8 [isLeaf=0];
9 [isLeaf=1];
1--3  [time=3.41843];
1--4  [time=1.01826];
4--5  [time=4.2496];
2--6  [time=5.59155];
1--6  [time=9.48199];
6--7  [time=7.72868];
0--8  [time=7.38977];
4--8  [time=6.55225];
8--9  [time=0.0406912];
}

我正在运行的代码:

#include <iostream>
#include <string>

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/property_map/dynamic_property_map.hpp>
#include <boost/graph/graph_utility.hpp>

struct Vertex
{
    bool isLeaf;
    std::string taxon;
};


typedef boost::property<boost::edge_weight_t, double> Edge;
typedef boost::adjacency_list<boost::vecS,boost::vecS, boost::undirectedS, Vertex, Edge> Graph;

int main()
{
    Graph g;

    boost::property_map<Graph, boost::edge_weight_t>::type weight = get(boost::edge_weight,g);  

    boost::dynamic_properties dp;
    dp.property("node_id", get(boost::vertex_index, g));
    dp.property("isLeaf", get(&Vertex::isLeaf, g));
    dp.property("time", weight);

    std::string blah = "Hello";

    std::ifstream dot("test.dot");

    boost::read_graphviz(dot,g,dp);

    g[1].taxon = blah;  
}

这个错误的原因是什么?

【问题讨论】:

    标签: c++ boost graph graphviz


    【解决方案1】:

    对 VertexContainerSelector 使用 vecS 意味着你有一个隐式的顶点索引描述符。它们隐含地是积分索引顶点描述符¹,恰好是顶点容器的积分索引。

    因此,标准的vertex_index 属性是只读的

    思想实验:给它写信意味着什么

    如果您更改了顶点的vertex_index 属性,它是否应该在内存中物理移动?如果它覆盖现有的顶点怎么办?旧址怎么办?容器应该被压实吗?但这意味着将顶点索引分配给单个顶点可能会更改所有其他顶点索引等)

    您可以通过多种方式解决此问题。最简单的方法似乎是在 Vertex 包中添加一个字段,以包含从输入中读取的 node-id 属性:

    #include <iostream>
    #include <string>
    
    #include <boost/graph/adjacency_list.hpp>
    #include <boost/graph/graph_traits.hpp>
    #include <boost/graph/graphviz.hpp>
    #include <boost/property_map/dynamic_property_map.hpp>
    #include <boost/graph/graph_utility.hpp>
    
    struct Vertex {
        int id;
        bool isLeaf;
        std::string taxon;
    };
    
    
    typedef boost::property<boost::edge_weight_t, double> Edge;
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, Vertex, Edge> Graph;
    
    int main()
    {
        Graph g;
        boost::property_map<Graph, boost::edge_weight_t>::type weight = get(boost::edge_weight,g);  
    
        boost::dynamic_properties dp;
        dp.property("node_id", get(&Vertex::id, g));
        dp.property("isLeaf", get(&Vertex::isLeaf, g));
        dp.property("time", weight);
    
        std::string blah = "Hello";
    
        std::ifstream dot("test.dot");
    
        boost::read_graphviz(dot,g,dp);
    
        g[1].taxon = blah;  
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-08
      • 2016-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多