C++ - dynamic pointer of array
first i would like to say i am Newbie in C++.
As part of my master thesis i am writing a program in C++ which also get as parameters the variables m
and d
(both integers). Were d
is the power of 2 (this means 2^d
elements开发者_JS百科). Parameter m
define the number of possible interactions between one element and the total group (2^d
elements).
The number of possible interactions is computed as following:
\kappa = \sum_{i=0}^m\binom{d}{i}
(at present i generate vector of vectors for 2^d
x \kappa, but my Prof. would like me to create different statistics to different m
's. My first though was to to generate a dynamic array of m
arrays of different sizes... Then i though of defining a 3-dim array with the biggest needed 2d array, but also program speed is important (e.g d = 20
).
i would like to ask for your advice how to define such kind of dynamic array that will also be fast.
Regards
Use boost mutlidimensional arrays
http://www.boost.org/doc/libs/1_41_0/libs/multi_array/doc/index.html
for example look at this code. This is very easy
#include "boost/multi_array.hpp"
#include <cassert>
int main () {
// Create a 3D array that is 3 x 4 x 2
typedef boost::multi_array<double, 3> array_type;
typedef array_type::index index;
array_type A(boost::extents[3][4][2]);
// Assign values to the elements
int values = 0;
for(index i = 0; i != 3; ++i)
for(index j = 0; j != 4; ++j)
for(index k = 0; k != 2; ++k)
A[i][j][k] = values++;
// Verify values
int verify = 0;
for(index i = 0; i != 3; ++i)
for(index j = 0; j != 4; ++j)
for(index k = 0; k != 2; ++k)
assert(A[i][j][k] == verify++);
return 0;
}
精彩评论