Copy Contents of One 2d Vector to Another 2d Vector

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2008
Posts: 17
Reputation: daniel88 is an unknown quantity at this point 
Solved Threads: 0
daniel88 daniel88 is offline Offline
Newbie Poster

Copy Contents of One 2d Vector to Another 2d Vector

 
0
  #1
May 3rd, 2008
Hi all,

This is my maiden post on not only this forum, but in fact any programming forum so please bear with me. I must say that this question does relate to a piece of homework, but it is the final member function in a series of about 8 and I will be posting all of my own code for you.

OK. The specs required me to read in a file which consisted of a number of rows and a number of columns, as well as a series of 0s and 1s (stored as bools). I was provided with a header file which contained a series of member functions to be implemented as well as three private data members:

1 - 2 variables to store rows and columns, respectively
2 - a 2d vector of vector< bool> - grid <vector <bool> >

The final member function rotates a "bitmap", which for the purposes of this assignment is a very simple piece of ascii art. Here is the problem:

*Everything* works, except for the fact that when I try to copy the contents of a temporary 2d vector I have created to manipulate the 1d vector, it fails and throws a range error. Let me show you the code:

  1. void bitmap::rotate()
  2. {
  3.  
  4. vector<vector <bool> > Vec; // vector created for the purpose of rotating contents of grid
  5.  
  6. vector <bool> Vec1; // 1d vector for pushing each column of original vector onto Vec temporarily before assigning back to grid
  7.  
  8. for (int i = 0; i < numcols; i++) // outer loop controls column number which will stay the same since we are cycling through the rows in this case
  9. {
  10. for (int j = 0; j < numrows; j++) // inner loop cycles through rows, keeping column the same
  11. {
  12. Vec1.push_back(grid.at(numrows - 1 - j).at(i)); // push the value at the bottom left of original vector onto top left of the new vector
  13. }
  14.  
  15. Vec.push_back(Vec1); // push entire new 1d vector onto Vec
  16. Vec1.clear(); // clear Vec1 so that it is ready for next iteration
  17. }
  18.  
  19. for (int k = 0; k < numcols; k++) // this is simply for debugging to demonstrate that Vec contains the correctly rotated image
  20. {
  21. for (int l = 0; l < numrows; l++)
  22. {
  23. if (Vec.at(k).at(l) == 0)
  24. cout << " ";
  25. else
  26. cout << "*";
  27. }
  28.  
  29. cout << endl;
  30.  
  31. }
  32.  
  33. grid = Vec;
  34.  
  35. }

I am really at the end of my tether because I can not think of a single thing to try. I have tried clearing grid and resizing and then copying the individual elements over. This also did not work. Any help would be extremely greatly appreciated. If I have posted too much information or in an annoying format, please tell me so that I do not make the same mistake twice.

Kind regards,

Daniel
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,812
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Copy Contents of One 2d Vector to Another 2d Vector

 
0
  #2
May 3rd, 2008
  1. void bitmap::rotate()
  2. {
  3.  
  4. vector<vector <bool> > Vec; // vector created for the purpose of rotating contents of grid
  5.  
  6. vector <bool> Vec1; // 1d vector for pushing each column of original vector onto Vec temporarily before assigning back to grid
  7.  
  8. for (int i = 0; i < numcols; i++) // outer loop controls column number which will stay the same since we are cycling through the rows in this case
  9. {
  10. for (int j = 0; j < numrows; j++) // inner loop cycles through rows, keeping column the same
  11. {
  12. Vec1.push_back(grid.at(numrows - 1 - j).at(i)); // push the value at the bottom left of original vector onto top left of the new vector
  13. }
  14.  
  15. Vec.push_back(Vec1); // push entire new 1d vector onto Vec
  16. Vec1.clear(); // clear Vec1 so that it is ready for next iteration
  17. }
  18.  
  19. for (int k = 0; k < numcols; k++) // this is simply for debugging to demonstrate that Vec contains the correctly rotated image
  20. {
  21. for (int l = 0; l < numrows; l++)
  22. {
  23. if (Vec.at(k).at(l) == 0)
  24. cout << " ";
  25. else
  26. cout << "*";
  27. }
  28.  
  29. cout << endl;
  30.  
  31. }
  32.  
  33. grid = Vec;
  34.  
  35. }

How far do you get before you get the out-of-range error (i.e. what line number above, what value of i and j, and what are numcols and numrows)? Do you get the range error when numcols and numrows are the same or only when they are different? If you don't get the error when numrows = numcols, but you do get it when they are not equal, chances are that somewhere you have mixed up numrows and numcols, or somewhere you have mixed up i and j. And I assume that numrows and numcols are set correctly and correctly reflect the data in the grid 2-dimensional vector?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 17
Reputation: daniel88 is an unknown quantity at this point 
Solved Threads: 0
daniel88 daniel88 is offline Offline
Newbie Poster

Re: Copy Contents of One 2d Vector to Another 2d Vector

 
0
  #3
May 3rd, 2008
Thank you so much!!! It's funny how sometimes you need someone to change the way you are looking at the problem. I realised after I thought about a number of the possibilities you raised what the problem was. Whilst I had made the new 2d vector the right size I had not changed the data members to reflect this so that when another one of my functions tried to display the contents of grid vector it threw a range error. It always seems childishly simple (which to you guys it is!) when you work it out. Thank you so much for your help. This web site is incredible. I cannot believe how quickly you helped me resolve my problem. Thanks again!!!
Daniel
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,812
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Copy Contents of One 2d Vector to Another 2d Vector

 
0
  #4
May 3rd, 2008
You're welcome, I'm glad you were able to solve your problem, and welcome to Daniweb.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC