Archived

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

Naku

vector STL

Recommended Posts

Can somebody help me. I was wondering if it was possible to a 2D array from the vector STL libary and if it is how do I do it? If it isn''t possible then could you please tell me how to dynamicly allocate memmory for a 2D array using malloc or new. Thanx in advance. - *NAKU*

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yes it is possible to do a 2d array with vectors.
I haven''t worked with them for a while, but I seem to recall that it goes something like this...

typedef vector X;
vector Q;

then you have a vector of vectors of ints... try playing around with that til you get it.


With 2D arrays the code might look like this...

char **Whatever;
Whatever = (char**)malloc(sizeof(char)*3); /*where 3 is the number of elements*/
for(int i = 0; i < 3; i++) /*3 is same number as line above*/
{
Whatever = (char*)malloc(sizeof(char)*50);
//here 50 is the number of characters in each slot of array*/
}

Hope that helps,
-laotzu

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yes it is possible to do a 2d array with vectors.
I haven''t worked with them for a while, but I seem to recall that it goes something like this...

typedef vector X;
vector Q;

then you have a vector of vectors of ints... try playing around with that til you get it.


With 2D arrays the code might look like this...

char **Whatever;
Whatever = (char**)malloc(sizeof(char)*3); /*where 3 is the number of elements*/
for(int i = 0; i < 3; i++) /*3 is same number as line above*/
{
Whatever = (char*)malloc(sizeof(char)*50);
//here 50 is the number of characters in each slot of array*/
}

Hope that helps,
-laotzu

Share this post


Link to post
Share on other sites
Sorry about the 3 posts... I''m seeming to have some trouble here, but hopefully this one comes out looking right

Yes it is possible to do a 2d array with vectors.
I haven''t worked with them for a while, but I seem to recall that it goes something like this...

typedef vector< int > X;
vector< X > Q;

then you have a vector of vectors of ints... try playing around with that til you get it.


With 2D arrays the code might look like this...

char **Whatever;
Whatever = (char**)malloc(sizeof(char)*3); /*where 3 is the number of elements*/
for(int i = 0; i < 3; i++) /*3 is same number as line above*/
{
Whatever = (char*)malloc(sizeof(char)*50);
//here 50 is the number of characters in each slot of array*/
}

Hope that helps,
-laotzu

Share this post


Link to post
Share on other sites