简体   繁体   English

在C / C ++中使用多维数组是不好的做法吗?

[英]Is it bad practice to use multi-dimensional arrays in C/C++?

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? 使用多维数组是不好的做法,还是无关紧要?

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 (ie, 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. 两种方法的复杂性随着维数的增加而增加,但是,在我看来,由于额外的间接水平,后一种方法的增长速度要快得多。

Advantages of multi-dim arrays to Vector<Vector<>> 多维数组阵列对Vector<Vector<>>优点

  1. Easy to type [ ][ ] 易于输入[] []
  2. C compatible. C兼容。
  3. Simple to understand conceptually what is it is doing. 简单地从概念上理解它在做什么。

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. Jagged arrays require care to set up. 锯齿状阵列需要注意设置。 Vector pattern is easy. 矢量模式很容易。
  3. 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. 大多数时候,我已经看到它们只是作为原始地址传递给双指针,这会破坏编译器为您做的内在数学运算。

Basically though, it comes down to lack of bounds checking for me. 基本上,它归结为缺乏对我的界限检查。

There are following advantages of multi-dimensional arrays over Vector<Vector<>> : 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. 它们很容易打字。

没有它,你会如何实现我最喜欢的算法

Well, in C++ I dislike multidimensional arrays because they should be replaced with std::vector<std::vector<t> > . 好吧,在C ++中我不喜欢多维数组,因为它们应该用std::vector<std::vector<t> >替换。 They're also particularly important if you want to represent a std::vector<std::basic_string<t> > . 如果你想表示一个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. 但是,使用单个维度的设计可能比使用多维度的设计更好,所有其他条件相同。

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). 如果您正在使用多维数据,那么我觉得最适合为作业选择正确的工具(多维数组)。

如果多维索引计算错误, std::valarraystd::slice是标准的抽象。

I can recommend Boost.MultiArray. 我可以推荐Boost.MultiArray。 Boost.MultiArray provides a generic N-dimensional array concept definition and common implementations of that interface. Boost.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

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. 请考虑不仅将字符串存储在一维数组中(可能使用某种分隔符),还要执行排序等操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM