-1

After searching for a while I did not find an answer to my question, so apologies in advance if this was already answered somewhere else.

I'm looking for a multi-dimensional data-structure in C++ that allows access not only as N-dimensional array, but also as 1-dimensional.

For an example assume a simple 2-dimensional matrix (it could go to higher dimensions, but in that case let's stick to this example). In most cases the member will be accessed in row-colum-form, e.g. matrix[x][y]. In other cases it might be desireable to access all members as a single list, e.g. for matrix addition using std-algorithms.

Standard-Approach would probably be something like std::array<std::array<double, 4>, 4> and write additionally an iterator with linear access to all members and maybe an extra accessor function.

A second approach is the other way around std::array<double, 16> with accessors in row-colum-form, but in this case it gets tricky to return whole columns .

Or maybe it is doable with boost MultiArray, but I think reducing the dimensions of a MultiArray always results in only getting slices of the MultiArray.

My question boils down to: Is there already an implementation in the standard-library or some well-known library, like boost, for this? If not, am I missing a point and there is a simpler method than the ones I wrote about?

EDIT: I was not looking for only iteration over all values, like in the mentioned question. But however from the pointed documentation I could find that MultiArray can be accessed as C-style array which is enough for my needs. This can then be closed and thanks for all answers

user1781290
  • 2,674
  • 22
  • 26
  • I found this nice blog on google, http://cpptruths.blogspot.com/2011/10/multi-dimensional-arrays-in-c11.html – pyCthon Jan 12 '14 at 22:58
  • There should be nothing like this in the standard. Your choices are either to role out your own solution, find a library such as boost, or use the simpler methods you've mentioned. – pyCthon Jan 12 '14 at 23:01
  • Answers from [Setting pointer to arbitrary dimension array?](http://stackoverflow.com/questions/19709529/setting-pointer-to-arbitrary-dimension-array/19725907#19725907) may help you. – Jarod42 Jan 13 '14 at 10:28

2 Answers2

2

See boost::multi_array::data() and boost::multi_array::num_elements().

As with std::vector, it would appear you can just access it as a solid block of memory by index, if that's all you want. I've never done this, but looks like you can. Just because you can doesn't necessarily mean you should, but, well...

See this answer:

how to traverse a boost::multi_array

Community
  • 1
  • 1
1

There is something like what you are looking for: std::valarray<T>. Well, the intend of the std::valarray<T> class template is to provide different views on the same array and to support potentially vectorized evaluation. That said, it doesn't really work and probably few people are using it.

However, from what you described you probably want to have something providing an array view on an existing array. I'd be pretty sure that this was implemented before, if nothing else as a replacement for std::valarray<T> but I can't point to an implementation.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380