|
-
April 29th, 2004, 03:25 PM
#1
Try with 2d vector catch the error
I declare my 2d vector as follows:
Code:
std::vector<std::vector<int> > mattrix_
in try-catch block and I receive the error caught from catch(...), I make this question just to confirm if it is of necessity for me to initialize that 2d vector at the same time in the try block...Hopefully I can receive any yes-or-nos or any comments from you...Sorry for such a question since I am really new to this.
Thanks a lot in advance,
Regards,
-Milano
Last edited by Milano; April 29th, 2004 at 03:29 PM.
-
April 29th, 2004, 04:08 PM
#2
Umm.. I don't think I understand what you're asking, but there's nothing wrong with the single line of code you posted. (Except from a missing ';' that is!) And certainly nothing that should end up in an exception being thrown.
Could you please try to ******** what you are asking?
-
April 29th, 2004, 04:53 PM
#3
Show us more of your code.
Make sure you do a resize for each iteration.
Example:
PHP Code:
std::vector< std::vector< int > > vec_2dArray(Q_Col);
for (int i = 0;i < Q_Col;++i) vec_2dArray[i].resize(Q_Row);
for (int t = 0;t < QtyTestItera;++t)
for(int i0 = 0;i0 < Q_Col;++i0)
{
for(int i1 = 0;i1 < Q_Row;++i1)
{
vec_2dArray[i0][i1] = i0 + i1;
}
}
-
April 29th, 2004, 04:57 PM
#4
I recommend using the following class for 2D arrays.
PHP Code:
template < class T>
class dynamic_2d_array
{
public:
dynamic_2d_array(int row, int col):m_row(row),m_col(col), m_data((row!=0&&col!=0)?new T[row*col]:NULL){}
~dynamic_2d_array(){if(m_data) delete []m_data;}
inline T* operator[](int i) {return (m_data + (m_col*i));}
inline T const*const operator[](int i) const {return (m_data + (m_col*i));}
private:
const int m_row;
const int m_col;
T* m_data;
};
Declare it like this:
PHP Code:
dynamic_2d_array<int> My2DArray(33, 44);
You can then reference it just like a C-Style 2D array
My2DArray[1][22] = 3;
-
April 29th, 2004, 05:44 PM
#5
Thanks a lot for your answers
Actually, I'd like to use 2d vector to read the content of my file, but when I declared it as above (Sorry, I made a typo, without , my program couldnot read anything, I initialized it again and it worked fine then, thats why I made that question just for confirmation, thats all...
Thanks again,
-Milano
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|