tags:

views:

99

answers:

2

I am trying to use the scythe statistical library (found here: http://scythe.wustl.edu/). I can initialize a matrix just fine with:

Matrix<double> A(2, 2, false);

But I would like to have a dynamic array of such matrices. Does anyone have any hints? Do I use vector? If so how?

Many thanks!

+5  A: 

A std::vector would be an excellent choice, especially if you don't know until runtime how many matrices you need. For example,

std::vector<Matrix<double> > vectorOfMatrices;

vectorOfMatrices.push_back(Matrix<double>(2, 2, false));
// etc.
James McNellis
@James, yep. That's perfect. As you can tell, I'm a bit of a newb. Thanks so much!
TJB
A: 

There are two classes suitable for your task:

a) std::vector - stores the objects in an array, can be accessed via index as well as iterator. Good for random access operations.

b) std::list - stores objects as a linked-list, accessed via iterators. Good for sequential-only access and frequent modifications.

So, if you just want an array of fixed or only rarely-changing size, go for std::vector. However, if you know these will change rather often, and you typically iterate over the whole thing, std::list is a better alternative.

Mephane
@Mephane, thank you for your answer.
TJB