0
我正试图编写一个程序,其中在循环的每个步骤中,我创建了一个表示随时间变化的图形的邻接表。 下面的代码:覆盖向量<vector<>>和分段错误
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/uniform_real.hpp>
#include <boost/random/exponential_distribution.hpp>
using namespace std;
using std::vector;
typedef boost::mt19937_64 ENG; // use Mersenne Twister 19937 as PRNG engine
typedef boost::uniform_int<> DIST_INT; // define uniform distribution of integers
typedef boost::uniform_real<> DIST_REAL; // define uniform distribution of reals on [0,1)
typedef boost::exponential_distribution<> DIST_EXP; // define exponential distribution
typedef boost::variate_generator<ENG,DIST_INT> VARIATE_INT;
typedef boost::variate_generator<ENG,DIST_REAL> VARIATE_REAL;
typedef boost::variate_generator<ENG,DIST_EXP> VARIATE_EXP;
int main()
{
const unsigned int random_seed = time(NULL);
// ======= initialize BOOST machines
ENG eng(random_seed);
DIST_INT dist_int;
DIST_REAL dist_rand(0,1);
DIST_EXP dist_exp;
VARIATE_INT randint(eng,dist_int); //random integer. use as: randint(N)
VARIATE_REAL rand(eng,dist_rand); //random float on [0,1[. use as: rand()
VARIATE_EXP randexp(eng,dist_exp); //random exponentially distributed float.
int N = 500, Tmax=200000, v, w;
float p = 0.2, s;
vector<vector<int> > contact_list;
for(int i = 0; i < 200000; i++)
{
contact_list.resize(N, vector<int>());
v = 1;
w = -1;
while(v < N)
{
s = rand();
w += 1 + int(log(1-s)/log(1-p));
while((w >= v) && (v < N))
{
w = w - v;
v += 1;
}
if (v < N)
{
contact_list[v].push_back(w);
contact_list[w].push_back(v);
}
}
}
}
但是在某些时候,我得到段错误。事实上,我认为这可能不是覆盖矢量的正确方法。我还补充说我想在每一步中改变N_nodes。任何帮助表示赞赏!
您正在创建向量的新载体*通过循环*每次。我不明白这对你有什么好处。显然'N_nodes'有同样的问题。也许你应该在更高的范围内声明这些变量,比如循环的*外*。 –
问题出在您未发布的代码中。 – molbdnilo
欢迎使用堆栈溢出,请阅读[this](http://stackoverflow.com/help/mcve)以了解如何发布最小和完整的示例 – UKMonkey