Public Group

Archived

This topic is now archived and is closed to further replies.

Multidimensional Vectors?

This topic is 5916 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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!

Share on other sites
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 ]
Thanks to Kylotan for the idea!

Share on other sites
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!

Werdy666

Share on other sites
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 ]
Thanks to Kylotan for the idea!

Share on other sites
thanks Oluseyi! that is exactly what i wanted! Your a champ!

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

Werdy666

Share on other sites
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.

Share on other sites
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 ]
Thanks to Kylotan for the idea!

1. 1
Rutin
19
2. 2
3. 3
JoeJ
15
4. 4
5. 5

• 18
• 19
• 10
• 13
• 17
• Forum Statistics

• Total Topics
631694
• Total Posts
3001751
×