Look here:
vector<vector<int> > ret;
int upper = 10;
vector<int> v(entry, upper);
ret.push_back(v);
typedef vector<int>::iterator iter_t;
iter_t it = v.begin();
int count=0;
int c = 0;
while(v.back() != 6) {
v = ret[count+c];
while(it != v.end()) {
--(*it);
++it;
ret.push_back(v);
++c;
}
it = v.begin();
c=0;
++count;
}
This is just scary. (I understand that you're a beginner; please understand that my criticism is intended to help.) Usually this kind if dense complexity is unnecessary and serves as a hiding place for bugs. Notice that c
and it
are set before the loop and at the end of the loop, and never used again; we can set them at the beginning of the loop, and the code will be shorter and clearer:
int count=0;
while(v.back() != 6) {
iter_t it = v.begin();
int c = 0;
v = ret[count+c];
while(it != v.end()) {
--(*it);
++it;
ret.push_back(v);
++c;
}
++count;
}
Now we can see that c
is never used except when it's zero. (Look at the original code if you don't believe me.) But what's much worse is that it
points into v
, and then v
is assigned a new value. So it
probably points into dead memory, and dereferencing it causes undefined behavior. And it's not clear how this code is intended to work anyway.
Try this:
vector<int> v(n,6);
vector<int>::iterator itr1;
do{
ret.push_back(v);
itr1 = v.begin();
while(++(*itr1)>10){
if(++itr1==v.end())
break;
}
for(vector<int>::iterator itr2 = v.begin(); itr2!=itr1; ++itr2)
*itr2 = *itr1;
}
while(itr1!=v.end());