Reputation: 1827
Some programmers seem to violently hate them, while others seem to think they're fine. I know that anything that can be done to a multi-dimensional array can also be done to a regular array, so they're functionally equivalent. Is it bad practice to use multi-dimensional arrays, or does it not matter?
Upvotes: 19
Views: 8217
Reputation: 11
There are following advantages of multi-dimensional arrays over Vector<Vector<>>
:
Upvotes: 1
Reputation: 5148
I know that anything that can be done to a multi-dimensional array can also be done to a regular array
I do not think that's entirely accurate. We'll need an array of pointers to store something as basic as a list of names and then sorting it. Or pointers to pointers to store a variable length string and then a list of such strings. As the original questions mentions only arrays per se, can't see how problems like these can be done with equal ease in a regular array. Please consider not just storing the strings in a 1-D array (using some sort of separator maybe) but also performing operations such as sorting.
Upvotes: 0
Reputation: 6323
I can recommend Boost.MultiArray. Boost.MultiArray provides a generic N-dimensional array concept definition and common implementations of that interface.
http://www.boost.org/doc/libs/1_42_0/libs/multi_array/doc/index.html
Upvotes: 0
Reputation: 137860
If multidimensional index computation bugs you, std::valarray
with std::slice
is the standard abstraction.
Upvotes: 0
Reputation: 12515
Advantages of multi-dim arrays to Vector<Vector<>>
Disadvantages:
Basically though, it comes down to lack of bounds checking for me.
Upvotes: 4
Reputation: 112
It may be possible to store multi-dimensional data in a single-data array, but you have to keep track of the indexes yourself. Multi-dimensional arrays are actually stored in memory as a single dimensional array, with syntax to support representing that data as multi-dimensional.
If you are working with multi-dimensional data, then I feel it is most appropriate to choose the correct tool for the job (a multi-dimensional array).
Upvotes: 0
Reputation: 355167
Do you need to store multi-dimensional data where you know the dimensions ahead of time? If so, use a multi-dimensional array.
If you don't know the dimensions ahead of time (i.e., you're going to have to dynamically allocate the array), then you either need to either
It depends on the specific use case, but as a rule of thumb, I almost always prefer the former because it makes for less memory management hassle and fewer heap allocations. The complexity for both approaches grows as the number of dimensions increases, but, in my opinion, it grows much faster for the latter approach due to the extra levels of indirection.
Upvotes: 12
Reputation: 106589
Well, in C++ I dislike multidimensional arrays because they should be replaced with std::vector<std::vector<t> >
. They're also particularly important if you want to represent a std::vector<std::basic_string<t> >
.
Multidimensional arrays are so simple a primitive I'm suprised most would care. However, a design that uses a single dimension is probably better than one using multiple dimensions, all other things being equal.
Upvotes: 0