I’ve found that using a vector is the best way to achieve what I need to do, however I now need some clarification.
I need to generate a multidimensional array within a function in an external CPP, then make this available within main.
main.cpp
// include vector, using namespace etc.
function(2, 4);
// how to access vector elements here - vectorname[2][4]->classvar; ?
vectors.cpp
void function(value1, value2){
// class def
int value1 = value1;
int value2 = value2;
vector<int>(value1)<vector<Class>(value2) vectorname>; // incorrect syntax? or new * vector ?
return vectorname; // ?
}
The syntax with
vectorrequires a little getting used to. A 2D vector ofintlooks like this:If you would like to set specific dimensions, use constructors that take size:
This produces a
10x5vector of zeros. You can do thisto provide initial values for your elements (
-1in this case).As a rule of thumb, you want your vectors passed by reference and returned by value.