SAT SAT - 1 year ago 50
C++ Question

Storing an undefined number of matrices in c++

I am looking for a way to have a non-predetermined amount of matrices that can later be accessed automatically in C++. For example, I would like for the user to be able to input the values of a matrix as many times as they want. But then, I need to be able, to use each matrix by using a for loop to access each matrix.

So far I have no idea how I can do the second part and I have seen maps being used to do the first. However, as far as I can tell, a map of matrices won't allow me to call each matrix with a variable.

Any help is greatly appreciated.

Answer Source

If I can uderstand you clearly, you need a dynamic container, that allows you to store undetermined amount of objects (i.e. matrices). In C++ you can use std::vector<Object> container, which can be easily resized with objects of a specific type (in the example above - Object, in your case - a matrix).

Vector acts like a classic array when it comes to accessing its elements, just like you want to:

std::vector<Matrix> matrices;
matrices[0] = ..... // anything

Link to the reference:

However std::map container could do this as well.
A map of matrices could allow you to access to each matrix by its key, e.g. a std::string name

Your final solution depends on a purpose.