| | |
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:
Solved Threads: 0
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:
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
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:
C++ Syntax (Toggle Plain Text)
void bitmap::rotate() { vector<vector <bool> > Vec; // vector created for the purpose of rotating contents of grid vector <bool> Vec1; // 1d vector for pushing each column of original vector onto Vec temporarily before assigning back to grid 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 { for (int j = 0; j < numrows; j++) // inner loop cycles through rows, keeping column the same { 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 } Vec.push_back(Vec1); // push entire new 1d vector onto Vec Vec1.clear(); // clear Vec1 so that it is ready for next iteration } for (int k = 0; k < numcols; k++) // this is simply for debugging to demonstrate that Vec contains the correctly rotated image { for (int l = 0; l < numrows; l++) { if (Vec.at(k).at(l) == 0) cout << " "; else cout << "*"; } cout << endl; } grid = Vec; }
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
•
•
Join Date: Jan 2008
Posts: 3,812
Reputation:
Solved Threads: 501
C++ Syntax (Toggle Plain Text)
void bitmap::rotate() { vector<vector <bool> > Vec; // vector created for the purpose of rotating contents of grid vector <bool> Vec1; // 1d vector for pushing each column of original vector onto Vec temporarily before assigning back to grid 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 { for (int j = 0; j < numrows; j++) // inner loop cycles through rows, keeping column the same { 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 } Vec.push_back(Vec1); // push entire new 1d vector onto Vec Vec1.clear(); // clear Vec1 so that it is ready for next iteration } for (int k = 0; k < numcols; k++) // this is simply for debugging to demonstrate that Vec contains the correctly rotated image { for (int l = 0; l < numrows; l++) { if (Vec.at(k).at(l) == 0) cout << " "; else cout << "*"; } cout << endl; } grid = Vec; }
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?
•
•
Join Date: May 2008
Posts: 17
Reputation:
Solved Threads: 0
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
Daniel
![]() |
Similar Threads
- Copy an arrays contents into a Vector (STL) (C++)
- (another) big O question (Computer Science)
- Sorting a map<string,long> by the value... (C)
- Copy File Contents to an Array (C)
Other Threads in the C++ Forum
- Previous Thread: Linker Error (Undefined Reference)
- Next Thread: a lil help
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






