Sign in to follow this  
svnstrk

vector of array

Recommended Posts

svnstrk    133
hi, im having trouble try to implement an array of array which has different dimension. so eg i have 4 arrays int a[5] int b[6] int c[2] int d[7] and i want to put this into an array. its not possible to do it in multimdimensional array since the array must have the same length. my idea is to put it into a vector of arary but when i try to run this program
vector<double*> arrayvec;
int main (int argc, char * const argv[]) {
	
        // yes one million items
	double a[1000000];
	arrayvec.push_back(a);
	//std::cout << arrayvec[0][6];
}


it gives me error not enough memory. but when i remove the push back command, i can even put double the amount. any idea why this is happen and how can i tackle it? thanks in advance

Share this post


Link to post
Share on other sites
alvaro    21266
Quote:
Original post by rip-off
It sounds like you want a jagged array. Why not use std::vector<std::vector<int> >?


Hmmm... I think vector<vector<int> > runs the risk of moving around large amounts of memory if the outer vector needs to grow its space. I believe there are plans to make the STL have move semantics, but for the time being it might be safer to use deque<vector<int> > instead. Of course, it depends a lot on the usage, what is known at construction time, etc.

Share this post


Link to post
Share on other sites
TyphoidHippo    122
Perhaps I've missed something in the question, but how about just this:

double** array2D = new double*[500];

array2D[0] = new double[1000000];
array2D[1] = new double[2];
array2D[2] = new double[500];

It's not really a tightly-packed array, I guess, but rather an array of pointers to arrays, but an std::vector of arrays would, also, not necessarily be a contiguous array. Personally I'd use std::vector<std::vector<double>> if for no other reason than the handy size() member of all the randomly sized arrays... but if it's giving you problems, then by all means - get nasty with it

Edit: Like _fastcall said - I suspect you are getting the out-of-memory error because you are making that array of 1 million doubles on the stack, which may well simply be too much. This might work:
double* array0 = new double[1000000];
arrayVector.push_back(array0);

Of course, remember to delete[] arrayVector[0] before popping it


[Edited by - TyphoidHippo on March 18, 2010 6:26:01 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this