Multidimensional Vectors?

Started by
7 comments, last by werdy666 21 years, 11 months ago
hi, I have recently discovered the world of STL vectors and want to try to learn them. Multidimensional vectors seem strange to me. I have a program that used arrays but I am trying to change it to vectors. This is what i have for my array..... array[x][80] the x is the amount of words in a list I load into my program. and the 80 is the space for each word. So i access each word by just ''array[x]'' which gives me the whole word. Now vectors are a little different. I am not sure i understand how to access both the [x] and [y] of the vector. I found this code on the gametutorials boards vector< int > v; vector< int > v2; vector< vector< int > > vvv; for(int i = 0; i < 5; i++) { v.push_back(i); vvv.push_back(v); Could someone explain to me how i access it like the way i do in my original array? I''ve found i have to do it something like (vvv[x],v[y]) it seems to work. although i am unsure if that is the x and y. Is the v the 2nd dimension and the vvv the first dimension? Hopefully someone will understand my gibberish! lol And thanks for your time and help!
Advertisement
vvv[x][y]

vvv[x] returns a vector of integers, to which you can again apply the [] operator to gain access to a specific integer. A good rule of thumb is that you can access vectors (and vectors of vectors, and vectors of vectors of vectors, etc ad nauseum) just like you would arrays.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
ok thats the way i thought you should be able to access them but i get a runtime error whenever i try to access the vector like that

the error is

Unhandled exception at 0x00412437 in vector.exe: 0xC0000005: Access violation writing location 0x00000000

and here is my code

int main()
{
vector< vector< char > > vvv;
vvv.resize(80);
for(int i = 0; i < 10; i++)
{
for (int j = 0; j< 10; j++)
{
vvv[j] = ''a'';
}
}

return 0;
}

I know i am doing something wrong but i don''t know what!

Thanks for your reply Oluseyi

Werdy666
vvv.resize(80); only resizes the major dimension; it doesn''t create an 8x10 or 10x8 or other multidimensional array. You need to resize each of those 80 elements.

Do this instead; it eliminates the need to know the dimensions ahead of time:
vector< vector< char > > vvv;for( int i = 0; i < 5; ++i ){  vector< char > v;  for( int j = 0; j < 10; ++j )    v.push_back( j );   vvv.push_back( v );} 


[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
Why not use Matrices?


Chris ''coldacid'' Charabaruk <ccharabaruk@meldstar.com> <http://www.meldstar.com/~ccharabaruk/>
Meldstar Studios <http://www.meldstar.com/> - Creation, cubed.

This message double ROT-13 encrypted for additional security.

Chris 'coldacid' Charabaruk – Programmer, game designer, writer | twitter

quote:Original post by coldacid
Why not use Matrices?

a.) What is a matrix? (I really don''t expect you to answer that question)
b.) Is there a standard matrix class?

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
thanks Oluseyi! that is exactly what i wanted! Your a champ!

And yeah i thought a matrix was for 3d graphic stuff! lol

Werdy666
i will point out you can just access multi dimensional space linearly using a single vector.

basicall use the formula: (x+y*width) for acces to to element at x,y. buffer size is calculated as width*height. might be better suited for your app, may not be. good option to keep open.
quote:Original post by a person
i will point out you can just access multi dimensional space linearly using a single vector.

Allow me to reiterate that this is merely simulating multidimensional access, and is not recommended for the newbie (simply because it''s more prone to error). You and a few others bring this up every single time multidimensional arrays are mentioned; it really doesn''t help as it isn''t necessarily faster or more logical. Once it was.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!

This topic is closed to new replies.

Advertisement