-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
r0d wrote:
Hello everybody,
i'm looking for the best way to modify the dimensions of an array. For
example, i got this one:
boost::multi_array array ( boost::extents[3][3] );
and i want to change it in an array of 5 dimensions.
Now, what i'm doing is to create a new array of 5 dimensions and copying the
first one into it, then delete the first one. But i'm sure that a more
efficient way exists. No?
use resize() - see below
TEST(multi_array)
{
boost::multi_array myArray ( boost::extents[3][3] );
int values = 0;
for(boost::multi_array::index i = 0; i != 3; ++i)
{
for(boost::multi_array::index j = 0; j != 3; ++j)
{
myArray[i][j] = values++;
}
}
// Verify values
int verify1 = 0;
for(boost::multi_array::index i = 0; i != 3; ++i)
{
for(boost::multi_array::index j = 0; j != 3; ++j)
{
CHECK_EQUAL(myArray[i][j],verify1++);
}
}
myArray.resize(boost::extents[5][5]);
//continue setting 'values' in the newly added array space
for(boost::multi_array::index i = 3; i != 5; ++i)
{
for(boost::multi_array::index j = 3; j != 5; ++j)
{
myArray[i][j] = values++;
}
}
//check the original data is still there
int verify2 = 0;
for(boost::multi_array::index i = 0; i != 3; ++i)
{
for(boost::multi_array::index j = 0; j != 3; ++j)
{
CHECK_EQUAL(myArray[i][j],verify2++);
}
}
//check that the new array space has correct values
for(boost::multi_array::index i = 3; i != 5; ++i)
{
for(boost::multi_array::index j = 3; j != 5; ++j)
{
CHECK_EQUAL(myArray[i][j],verify1++);
}
}
}
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org
iD8DBQFIQGnW/X5Z5iyVCbERAmCmAJ4qgpbHzcUYqGor8kDUabQfDHwCLQCfZ7EH
dTGc5PybZhGR0zgyUxx92q0=
=VwiS
-----END PGP SIGNATURE-----