#### Archived

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

# vector STL

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

## 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 on other sites
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 on other sites
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 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

1. 1
2. 2
3. 3
frob
12
4. 4
5. 5

• 13
• 14
• 65
• 14
• 15
• ### Forum Statistics

• Total Topics
632130
• Total Posts
3004286

×