C++ 2d vectors Issue inside a "for" loop
I understand there are issues when you try to change size of 2d vector objects but I'm facing a rather weird issue here. Code fragment is attached below :
for (int iter = 0; iter < J; iter++) {
cout << "iter " << iter << endl;
rows_n = rows_n / 2;
cols_n = cols_n / 2;
cout << "sig2 " << sig2.size() << endl;
vector<vector<double> > cA(rows_n, vector<double>(cols_n));
vector<vector<double> > cH(rows_n, vector<double>(cols_n));
vector<vector<double> > cV(rows_n, vector<double>(cols_n));
vector<vector<double> > cD(rows_n, vector<double>(cols_n));
cout << "cA " << cA.size() << endl;
ANALYSIS_FB(nm,sig2,cA,cH,cV,cD);
sig2.clear();
vector<vector<double> > sig2(rows_n, vector<double>(cols_n,0.0));
sig2 = cA;
for(int i =0; i < rows_n; i++){
for (int j =0; j < cols_n; j++){
dwt_output[i][j]=cA[i][j];
}
}
for(int i =0; i < rows_n; i++){
for (int j = cols_n; j < cols_n * 2; j++){
dwt_output[i][j]=cH[i][j - cols_n];
}
}
for(int i = rows_n; i < rows_n * 2; i++){
for (int j =0; j < cols_n; j++){
dwt_output[i][j]=cV[i - rows_n][j];
}
}
for(int i = rows_n; i < rows_n * 2; i++){
for (int j = cols_n; j < cols_n * 2; j++){
dwt_output[i][j]=cD[i- rows_n][j - cols_n];
}
}
for(int i = 0; i < rows_n; i++){
for (int j = 0; j < cols_n ; j++){
cout <开发者_JAVA技巧< sig2[i][j] << " ";
}
cout << endl;
}
cA.clear();
cH.clear();
cV.clear();
cD.clear();
cout << "sig2e " << sig2.size() << endl;
}
As you can see this is part of a much larger code but my problem is that 2d vector vanishes after the first iteration of the loop. I'm attaching the output from the console as well.
iter 0
sig2 16
cA 8
4.49991 10.4998 16.4997 22.4996 28.4995 34.4993 40.4992 46.4991 8.99983 20.9996 32.9994 44.9991 56.9989 68.9987 80.9984 92.9982 13.4997 31.4994 49.4991 67.4987 85.4984 103.498 121.498 139.497 17.9997 41.9992 65.9987 89.9983 113.998 137.997 161.997 185.996 16.4997 38.4993 60.4988 82.4984 104.498 126.498 148.497 170.497 14.9997 34.9993 54.9989 74.9986 94.9982 114.998 134.997 154.997 16.4997 38.4993 60.4988 82.4984 104.498 126.498 148.497 170.497 13.4997 31.4994 49.4991 67.4987 85.4984 103.498 121.498 139.497
sig2e 8 (My Note - First Iteration Ends with sig2's number of rows = 8)
iter 1 ( My Note - Second Iteration Begins)
sig2 0 (My Note- sig2 's number of rows = 0)
cA 4
As you can see, sig2 takes the value of cA in the first iteration and has the size 8 X 8 just before the end of the loop is reached. For the next iteration (iter value of 1), the 2d vector becomes a null vector. To be fair , I'm clearing the vector in the first iteration , followed by redefining it to be a smaller size but I still find the behavior to be a bit odd. Is there a way around this? The function ANALYSIS_FB takes the value of sig2 and returns cA,cH etc. sig2 's value is set to cA (which has dimensions half of the original sig2 in both directions) and so on.
Edit : Thank you guys for pointing out that sig2 was a global as well as the local variable. I had hit the wall and that helped me solve the problem. I ended up removing all the "clear" functions and getting rid of any additional local variables. I am using sig2 only as the global variable and the program is working.
You have 2 varibales named sig2, one outside of the loop and one inside !
From your code :
///You clear a global sig2
sig2.clear();
///You create a local sig2 !
vector<vector<double> > sig2(rows_n, vector<double>(cols_n,0.0));
精彩评论