加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何在boost图库中使用带有捆绑属性图的`randomize_propert

发布时间:2020-12-16 07:25:09 所属栏目:百科 来源:网络整理
导读:在文档中: http://www.boost.org/doc/libs/1_46_1/libs/graph/doc/random.html#randomize_property 只有一个函数原型,我找不到一个有效的例子. 我尝试了几件事,但它无法编译. 这是一个简单的源代码: #include ctime#include iostream#include boost/graph/
在文档中: http://www.boost.org/doc/libs/1_46_1/libs/graph/doc/random.html#randomize_property

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

#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;
};

原始编译错误发布为gist 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; // :)
}

希望有所帮助 – 我没有时间对它进行实际测试,所以如果这不是你想要的,那么道歉.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读