Reputation: 183
I'm new to Boost (and also to stackoverflow) and want to use a multiarray of vectors. I have done it that way:
typedef boost::multi_array<std::vector<Vector3_t>, 2> array_type;
array_type* mImage;
int mResolution = 1000;
mImage = new array_type (boost::extents[mResolution][mResolution]);
//works
mImage[0][0].origin()->push_back(Vector3_t());
//Error: abort()
mImage[1][1].origin()->push_back(Vector3_t());
//Error: abort()
mImage[500][440].origin()->push_back(Vector3_t());
On the internet I can only find examples of multiarray that use int,doule and so on. Is it possible to use a std::vector in a mutliarray ? I know I could use a 3d multiarray, but i would prefer vectors as elemet.
Upvotes: 1
Views: 1098
Reputation: 51881
Boost.MultiArray supports std::vector
elements. In general, Boost.MultiArray will perform concept checking at compile-time. Thus, if code compiles with the complete type, then it should be supported.
With mImage[0][0].origin()
:
mImage[0][0]
returns a reference to std::vector<Vector3_t>
.origin()
is not a member function on std::vector<Vector3_t>
, resulting in an error.origin()
is a member function of multi-array, that returns the address of the first element's storage. In the case where the array has not been reindexed to a positive index, this is equivalent of 0
for all indexes (i.e. mImage.origin() == &mImage[0][0]
).
Here is a brief and complete example with a multi-array of a vector of vector of ints.
#include <iostream>
#include <vector>
#include <boost/foreach.hpp>
#include <boost/range/counting_range.hpp>
#include <boost/multi_array.hpp>
int main()
{
typedef std::vector<int> vector3_type;
typedef boost::multi_array<std::vector<vector3_type>, 2> array_type;
array_type array(boost::extents[5][5]);
// Insert vector into multi-array.
array[0][0].push_back(vector3_type());
// Insert range of [100,105) into the first vector at [0][0]
BOOST_FOREACH(const int& i, boost::counting_range(100, 105))
array[0][0].front().push_back(i);
// Print all integers at [0][0][0]
BOOST_FOREACH(const int& i, array[0][0][0])
std::cout << i << std::endl;
}
And running produces the following expected output:
100 101 102 103 104
Upvotes: 3