在C/ c++中使用多维数组不好吗?

时间:2021-11-19 21:34:01

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?

一些程序员似乎非常讨厌它们,而另一些则认为它们很好。我知道任何可以对多维数组做的事情也可以对常规数组做,所以它们在功能上是等价的。使用多维数组是不好的做法,还是不重要?

9 个解决方案

#1


12  

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

如果你不提前知道维度(例如。,你将不得不动态分配数组),然后你或者需要

  • allocate a one-dimensional array and emulate an n-dimensional array using index arithmetic, or
  • 分配一个一维数组并使用索引算法模拟一个n维数组
  • allocate an array of pointers to arrays of elements to get actual multidimensional array semantics
  • 为元素数组分配一个指针数组,以获得实际的多维数组语义

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.

它取决于特定的用例,但是根据经验,我几乎总是喜欢前者,因为它减少了内存管理的麻烦和更少的堆分配。这两种方法的复杂性都随着维度的增加而增加,但是,在我看来,后一种方法由于额外的间接级别而增长得更快。

#2


4  

Advantages of multi-dim arrays to Vector<Vector<>>

多dim阵列对向量 >的优点

  1. Easy to type [ ][ ]
  2. 容易输入[][]
  3. C compatible.
  4. C兼容。
  5. Simple to understand conceptually what is it is doing.
  6. 很容易理解它在做什么。

Disadvantages:

缺点:

  1. No easily detected bounds checking. Bounding off the end of the outer brackets usually overflows into memory allocated by the inner brackets making these types of error a real pain to track.
  2. 没有容易检测到的边界检查。外括号末尾的边界通常会溢出到内括号分配的内存中,这使得跟踪这些类型的错误非常困难。
  3. Jagged arrays require care to set up. Vector pattern is easy.
  4. 交错数组需要小心设置。矢量模式是很容易的。
  5. Multidimensioal arrays are more than double pointers making it a pain to pass into functions correctly. Most of the time, I've seen them just passed as a raw address to a double pointer which defeats the intrinsic math the compiler will do for you.
  6. 多维数组不仅仅是双指针,因此要正确地传递到函数中非常困难。大多数时候,我看到它们只是作为一个原始地址传递给一个双指针,这会破坏编译器为你做的内在数学。

Basically though, it comes down to lack of bounds checking for me.

基本上,它归结为我缺乏边界检查。

#3


1  

There are following advantages of multi-dimensional arrays over Vector<Vector<>>:

多维数组比向量 >有以下优点:

  • They are easy to understand.
  • 它们很容易理解。
  • Searching and sorting of elements can be done very easily.
  • 元素的搜索和排序非常容易。
  • They are compatible with C.
  • 它们与C兼容。
  • They are easy to type.
  • 它们很容易打字。

#4


0  

How would you implement my favorite algorithm without it?

没有它你怎么实现我最喜欢的算法?

#5


0  

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> >.

在c++中,我不喜欢多维数组,因为它们应该被替换为std::vector <::vector> >。如果你想表示std::vector >。

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.

多维数组是如此简单的一种原始的,我被大多数人所关注。然而,在其他条件相同的情况下,使用单一维度的设计可能比使用多个维度的设计要好。

#6


0  

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).

如果您正在处理多维数据,那么我认为最适合为作业选择正确的工具(多维数组)。

#7


0  

If multidimensional index computation bugs you, std::valarray with std::slice is the standard abstraction.

如果多维索引计算错误,那么std::valarray和std::切片是标准的抽象。

#8


0  

I can recommend Boost.MultiArray. Boost.MultiArray provides a generic N-dimensional array concept definition and common implementations of that interface.

我可以推荐Boost.MultiArray。提振。MultiArray提供一个通用的n维数组概念定义和该接口的通用实现。

http://www.boost.org/doc/libs/1_42_0/libs/multi_array/doc/index.html

http://www.boost.org/doc/libs/1_42_0/libs/multi_array/doc/index.html

#9


0  

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.

我不认为这是完全正确的。我们需要一个指针数组来存储一些最基本的东西,比如名字列表,然后对其进行排序。或者指向指针的指针,用来存储可变长度字符串,然后是这样的字符串列表。由于最初的问题只涉及数组本身,所以在常规数组中无法看到这样的问题是如何同样容易地完成的。请考虑不仅将字符串存储在一个一维数组中(可能使用某种类型的分隔符),还要执行诸如排序之类的操作。

#1


12  

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

如果你不提前知道维度(例如。,你将不得不动态分配数组),然后你或者需要

  • allocate a one-dimensional array and emulate an n-dimensional array using index arithmetic, or
  • 分配一个一维数组并使用索引算法模拟一个n维数组
  • allocate an array of pointers to arrays of elements to get actual multidimensional array semantics
  • 为元素数组分配一个指针数组,以获得实际的多维数组语义

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.

它取决于特定的用例,但是根据经验,我几乎总是喜欢前者,因为它减少了内存管理的麻烦和更少的堆分配。这两种方法的复杂性都随着维度的增加而增加,但是,在我看来,后一种方法由于额外的间接级别而增长得更快。

#2


4  

Advantages of multi-dim arrays to Vector<Vector<>>

多dim阵列对向量 >的优点

  1. Easy to type [ ][ ]
  2. 容易输入[][]
  3. C compatible.
  4. C兼容。
  5. Simple to understand conceptually what is it is doing.
  6. 很容易理解它在做什么。

Disadvantages:

缺点:

  1. No easily detected bounds checking. Bounding off the end of the outer brackets usually overflows into memory allocated by the inner brackets making these types of error a real pain to track.
  2. 没有容易检测到的边界检查。外括号末尾的边界通常会溢出到内括号分配的内存中,这使得跟踪这些类型的错误非常困难。
  3. Jagged arrays require care to set up. Vector pattern is easy.
  4. 交错数组需要小心设置。矢量模式是很容易的。
  5. Multidimensioal arrays are more than double pointers making it a pain to pass into functions correctly. Most of the time, I've seen them just passed as a raw address to a double pointer which defeats the intrinsic math the compiler will do for you.
  6. 多维数组不仅仅是双指针,因此要正确地传递到函数中非常困难。大多数时候,我看到它们只是作为一个原始地址传递给一个双指针,这会破坏编译器为你做的内在数学。

Basically though, it comes down to lack of bounds checking for me.

基本上,它归结为我缺乏边界检查。

#3


1  

There are following advantages of multi-dimensional arrays over Vector<Vector<>>:

多维数组比向量 >有以下优点:

  • They are easy to understand.
  • 它们很容易理解。
  • Searching and sorting of elements can be done very easily.
  • 元素的搜索和排序非常容易。
  • They are compatible with C.
  • 它们与C兼容。
  • They are easy to type.
  • 它们很容易打字。

#4


0  

How would you implement my favorite algorithm without it?

没有它你怎么实现我最喜欢的算法?

#5


0  

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> >.

在c++中,我不喜欢多维数组,因为它们应该被替换为std::vector <::vector> >。如果你想表示std::vector >。

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.

多维数组是如此简单的一种原始的,我被大多数人所关注。然而,在其他条件相同的情况下,使用单一维度的设计可能比使用多个维度的设计要好。

#6


0  

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).

如果您正在处理多维数据,那么我认为最适合为作业选择正确的工具(多维数组)。

#7


0  

If multidimensional index computation bugs you, std::valarray with std::slice is the standard abstraction.

如果多维索引计算错误,那么std::valarray和std::切片是标准的抽象。

#8


0  

I can recommend Boost.MultiArray. Boost.MultiArray provides a generic N-dimensional array concept definition and common implementations of that interface.

我可以推荐Boost.MultiArray。提振。MultiArray提供一个通用的n维数组概念定义和该接口的通用实现。

http://www.boost.org/doc/libs/1_42_0/libs/multi_array/doc/index.html

http://www.boost.org/doc/libs/1_42_0/libs/multi_array/doc/index.html

#9


0  

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.

我不认为这是完全正确的。我们需要一个指针数组来存储一些最基本的东西,比如名字列表,然后对其进行排序。或者指向指针的指针,用来存储可变长度字符串,然后是这样的字符串列表。由于最初的问题只涉及数组本身,所以在常规数组中无法看到这样的问题是如何同样容易地完成的。请考虑不仅将字符串存储在一个一维数组中(可能使用某种类型的分隔符),还要执行诸如排序之类的操作。