|
Boost Users : |
From: Rupert Bruce (rupert_at_[hidden])
Date: 2008-05-30 16:55:50
-----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<int, 2> 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<int, 2> myArray ( boost::extents[3][3] );
int values = 0;
for(boost::multi_array<int, 2>::index i = 0; i != 3; ++i)
{
for(boost::multi_array<int, 2>::index j = 0; j != 3; ++j)
{
myArray[i][j] = values++;
}
}
// Verify values
int verify1 = 0;
for(boost::multi_array<int, 2>::index i = 0; i != 3; ++i)
{
for(boost::multi_array<int, 2>::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<int, 2>::index i = 3; i != 5; ++i)
{
for(boost::multi_array<int, 2>::index j = 3; j != 5; ++j)
{
myArray[i][j] = values++;
}
}
//check the original data is still there
int verify2 = 0;
for(boost::multi_array<int, 2>::index i = 0; i != 3; ++i)
{
for(boost::multi_array<int, 2>::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<int, 2>::index i = 3; i != 5; ++i)
{
for(boost::multi_array<int, 2>::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-----
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net