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'も同じ問題を抱えています。ループの* outside *のように、これらの変数をより高いスコープで宣言する必要がありますか? –
問題はあなたが投稿しなかったコードのどこかにあります。 – molbdnilo
スタックオーバーフローへようこそ[最小限の完全な例を投稿する方法については[こちら](http://stackoverflow.com/help/mcve)を参照してください) – UKMonkey