feelfree
feelfree

Reputation: 11753

Are there easy ways of increasing boost::multi_array dimension dynamically?

MultiArray in boost has a lot of advantages compared to creating multi-array with std::vector. However, one thing I feel uncomfortable with MultiArray in BOOST is that it is not easy to create a multi-array that can change its size easily. I have the following codes to make my point clear:

vector<vector<int> > my_2d_array;
vector<int> temp;
temp.push_back(3);
temp.push_back(4);
my_2d_array.push_back(temp);
temp.clear;
temp.push_back(4);
temp.push_back(5);
my_2d_array.push_back(temp);
temp.clear;
temp.push_back(41);
temp.push_back(51);
my_2d_array.push_back(temp);

As we can see from the above codes, using vector<vector<int> > data structure, we can increase the dimension of the multi-array very easily. But with boost::multi_array, you have to use boost::extents to determine the dimension of the multi-array before using it. I was wondering whether boost::multi_array also has a easy way of increasing its dimension as the vector<vector<int> > did in the above codes.

Upvotes: 1

Views: 523

Answers (1)

Delta_Fore
Delta_Fore

Reputation: 3271

myarray.reshape(extents[newSizeY][newSizeX]);

works. Incidentally you can't do something like

boost::multi_array<int,2> foo;
// Reshape first before the following call
foo = some_function_that_returns_multi_array()

which has caught me out a few times. You have to reshape

The advantage with using multi_array is that the data is stored in one contiguous block, which lends itself to better cache locality, but in "real-world" tests I've noticed that you can still reap significant performance improvements using simple std::array<> on occasion, but YMMV

Upvotes: 1

Related Questions