gpt4 book ai didi

c++ - 未能在 boost 库中使用 property_map 和 compressed_sparse_row_graph

转载 作者:搜寻专家 更新时间:2023-10-31 00:55:50 26 4
gpt4 key购买 nike

我已阅读boost docs弄清楚如何使用 property_map。

基于

// Property map accessors
template<typename PropertyTag>
property_map<compressed_sparse_row_graph, PropertyTag>::type
get(PropertyTag, compressed_sparse_row_graph& g)

我写了下面的代码:

#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/compressed_sparse_row_graph.hpp>
#include <boost/utility.hpp>

typedef boost::compressed_sparse_row_graph<boost::bidirectionalS, boost::no_property, boost::property<boost::edge_weight_t, int> > AdjGraph;
typedef typename boost::property_map<AdjGraph, boost::edge_weight_t>::type WeightMap;
class data {
WeightMap weight;
data()
{
std::vector<std::pair<int, int> > edges;
std::vector<int> edgesAttr;
boost::shared_ptr<AdjGraph> adjGraph;
adjGraph = boost::shared_ptr<AdjGraph>(new AdjGraph(boost::edges_are_unsorted_multi_pass, edges.begin(), edges.end(), edgesAttr.begin(), 0));
weight = boost::get(boost::edge_weight, *adjGraph);
}
};

int main() { return 0; }

但是编译的时候报错

我修改了

weight = boost::get(boost::edge_weight, *adjGraph);

成为

auto tmp = boost::get(boost::edge_weight, *adjGraph);

它编译得很好。

但是由于“weight”不应该是一个静态变量,“auto weight”是 Not Acceptable 。

我想知道“权重”应该是什么类型。我尝试了“typeinfo”和“typeid().name()”,但输出不可读。

虽然我引用了 1.61 文档,但我实际上使用的是 1.58 1.58 docs

最佳答案

I want to know what type "weight" should be

类型是WeightMap。你已经正确了。你正在解决错误的问题。这只是编译

WeightMap weight = boost::get(boost::edge_weight, *adjGraph);

那么问题是什么?

WeightMap 不可默认构造。与所有属性映射一样,它只是对实际数据(在本例中为图形模型内部)的轻量级、低成本可复制“引用”。

因此,没有理由将其存储在成员(member)中或与外界共享。

在更本质的层面上,因为属性映射通常(在这种情况下肯定)是对底层对象的引用,所以它的生命周期仅在底层图存在时才有效。

因此,将权重图保留在成员中没有意义,除非您还在较早的成员中保留指向该图的共享指针:

Live On Wandbox

#include <boost/graph/graph_traits.hpp>
#include <boost/graph/compressed_sparse_row_graph.hpp>
#include <boost/utility.hpp>

typedef boost::compressed_sparse_row_graph<boost::bidirectionalS, boost::no_property, boost::property<boost::edge_weight_t, int> > AdjGraph;
typedef typename boost::property_map<AdjGraph, boost::edge_weight_t>::type WeightMap;

class data {
boost::shared_ptr<AdjGraph> adjGraph;
WeightMap weight;
public:
data(std::vector<std::pair<int, int> > const& edges, std::vector<int> const& edgesAttr)
: adjGraph (boost::shared_ptr<AdjGraph>(new AdjGraph(boost::edges_are_unsorted_multi_pass, edges.begin(), edges.end(), edgesAttr.begin(), 0))),
weight(boost::get(boost::edge_weight, *adjGraph))
{
}
};

int main() {
std::vector<std::pair<int, int> > edges;
std::vector<int> edgesAttr;

data d(edges, edgesAttr);
}

关于c++ - 未能在 boost 库中使用 property_map 和 compressed_sparse_row_graph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41123421/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com