Use boost :: multi_array .
As in your example, the only thing you need to know at compile time is the number of dimensions. Here is the first example in the documentation:
#include "boost/multi_array.hpp"
Edit: as suggested in the comments, here is an example of a โsimpleโ example that allows you to determine the size of a multidimensional array at run time by asking the console entry. Here is a sample output from this sample application (compiled with a constant denoting its 3 dimensions):
Multi-Array test! Please enter the size of the dimension 0 : 4 Please enter the size of the dimension 1 : 6 Please enter the size of the dimension 2 : 2 Text matrix with 3 dimensions of size (4,6,2) have been created. Ready! Type 'help' for the command list. >read 0.0.0 Text at (0,0,0) : "" >write 0.0.0 "This is a nice test!" Text "This is a nice test!" written at position (0,0,0) >read 0.0.0 Text at (0,0,0) : "This is a nice test!" >write 0,0,1 "What a nice day!" Text "What a nice day!" written at position (0,0,1) >read 0.0.0 Text at (0,0,0) : "This is a nice test!" >read 0.0.1 Text at (0,0,1) : "What a nice day!" >write 3,5,1 "This is the last text!" Text "This is the last text!" written at position (3,5,1) >read 3,5,1 Text at (3,5,1) : "This is the last text!" >exit
Important parts of the code are the main function, where we get the sizes from the user and create an array with:
const unsigned int DIMENSION_COUNT = 3;
And you can see that to attach an element in an array is very simple: you just use the () operator, as in the following functions:
void write_in_text_matrix( TextMatrix& text_matrix, const Position& position, const std::string& text ) { text_matrix( position ) = text; std::cout << "Text \"" << text << "\" written at position "; display_position( position ); std::cout << std::endl; } void read_from_text_matrix( const TextMatrix& text_matrix, const Position& position ) { const std::string& text = text_matrix( position ); std::cout << "Text at "; display_position(position); std::cout << " : "<< std::endl; std::cout << " \"" << text << "\"" << std::endl; }
Note. I compiled this application in VC9 + SP1 - I received only some forgotten warnings.