D
David Kevin
Hi,
I would be grateful if somebody could explain why following code
causes assertions failures:
//#include "stdafx.h" //neccessary if you compile with VC++ 2008
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include "stdio.h"
using namespace std;
class A {
public:
A() {}
A(int currentDepth, int depth) { cout << currentDepth << " " << depth
<< "\n"; }
vector<A>* vecA;
};
vector<A> recursion(int fixedDepth) {
fixedDepth--;
getchar();
cout << "We are on level: " << fixedDepth << "\n";
vector<A> vecA;
int Max=rand() % 5+1;
for(int i=0; i<Max; i++) {
A a;
vecA.push_back(a);
}
vector<A>::iterator it;
for(it=vecA.begin(); it!=vecA.end(); it++) {
if(fixedDepth==0) {
if(it==vecA.end()) break;
it++;
continue;
}
it->vecA=new vector<A>;
*it->vecA=recursion(fixedDepth);
}
return vecA;
};
int main()
{
srand((unsigned)time(0));
vector<A> a=recursion(2);
return 0;
}
It is interesting that if one doesn't use random numbers but in for
loop there is single, known before run-time number instead of MAX it
seems to work nicely. My compiler is Visual C++ 2008.
Thanks in advance for responses,
Greetings.
I would be grateful if somebody could explain why following code
causes assertions failures:
//#include "stdafx.h" //neccessary if you compile with VC++ 2008
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include "stdio.h"
using namespace std;
class A {
public:
A() {}
A(int currentDepth, int depth) { cout << currentDepth << " " << depth
<< "\n"; }
vector<A>* vecA;
};
vector<A> recursion(int fixedDepth) {
fixedDepth--;
getchar();
cout << "We are on level: " << fixedDepth << "\n";
vector<A> vecA;
int Max=rand() % 5+1;
for(int i=0; i<Max; i++) {
A a;
vecA.push_back(a);
}
vector<A>::iterator it;
for(it=vecA.begin(); it!=vecA.end(); it++) {
if(fixedDepth==0) {
if(it==vecA.end()) break;
it++;
continue;
}
it->vecA=new vector<A>;
*it->vecA=recursion(fixedDepth);
}
return vecA;
};
int main()
{
srand((unsigned)time(0));
vector<A> a=recursion(2);
return 0;
}
It is interesting that if one doesn't use random numbers but in for
loop there is single, known before run-time number instead of MAX it
seems to work nicely. My compiler is Visual C++ 2008.
Thanks in advance for responses,
Greetings.