gpt4 book ai didi

c++ - 如何在 boost 图形库中使用 `randomize_property` 和捆绑属性图?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:13:08 25 4
gpt4 key购买 nike

在文档中:http://www.boost.org/doc/libs/1_46_1/libs/graph/doc/random.html#randomize_property

只有一个函数原型(prototype),我找不到一个有效的例子。我尝试了几件事,但就是无法编译。这是一个简单的源代码:

#include <ctime>
#include <iostream>
#include <boost/graph/random.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/random/linear_congruential.hpp>
#include <boost/graph/erdos_renyi_generator.hpp>
#include <boost/graph/graphviz.hpp>
using namespace std;
using namespace boost;

struct EdgeProperty {
int cost;
};

typedef adjacency_list<
setS, // disallow parallel edge
vecS,
undirectedS,
no_property,
EdgeProperty
> Graph;

typedef erdos_renyi_iterator<minstd_rand, Graph> ERGen;

int main(int argc, const char *argv[])
{
minstd_rand gen(time(0));
assert(argc >= 3);
int n = atoi(argv[1]);
double p = atof(argv[2]);
Graph g(ERGen(gen, n, p), ERGen(), n);

// randomize_property< [unknown class] >(g, gen);

return 0;
}

更新:@phooji 提供的代码有效。我为 EdgeProperty 添加了默认构造函数,我的代码也可以编译:

struct EdgeProperty {
EdgeProperty(int x = 0) : cost(x) { }
int cost;
};

原始编译错误作为要点发布 here ,我无法理解。希望有人告诉我这是如何工作的。

最佳答案

这为我编译:

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/random.hpp>
#include <boost/random/linear_congruential.hpp>

struct myedge {
myedge(int x) : testme(x) {
}
int testme;
};

typedef boost::adjacency_list<boost::setS, // disallow parallel edge
boost::vecS,
boost::undirectedS,
boost::no_property,
myedge
> mygraph;

int main(int argc, char**argv) {
mygraph g;

// auto pmap = boost::get(&myedge::testme, g);
boost::minstd_rand gen(0);
boost::randomize_property<boost::edge_bundle_t>(g, gen);
return EXIT_SUCCESS; // :)
}

希望对您有所帮助——我没有时间实际测试它,如果这不是您想要的,我们深表歉意。

关于c++ - 如何在 boost 图形库中使用 `randomize_property` 和捆绑属性图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5720650/

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