问题描述
我已经读到 vector
-of-vector
s 在给定固定的 2 维度的情况下很糟糕,但我找不到http://www.stackoverflow.com 上的问题的清晰解释.
I've read that a vector
-of-vector
s is bad given a fixed 2 dimension, but I cannot find a clear explanation of the problems on http://www.stackoverflow.com.
有人可以解释为什么在单个 vector
上使用 2D 索引比使用 vector
-of-vector
更好固定的 2 维?
Could someone give an explanation of why using 2D indexing on a single vector
is preferable to using a vector
-of-vector
s for a fixed 2 dimension?
另外,我假设 vector
-of-vector
s 是具有变量 2 dimension? If there is any proof to the contrary I'd love to see that.
推荐答案
我会用一个简单的类比来回答.
I shall answer with a simple analogy.
在这两件事中,一般来说更好"是什么?
What is "better" in general out of the two things?
- 一本电话簿,其中每个条目都是一个代码,指的是一本不同的书,您必须找到并阅读它才能发现某人的电话号码
- 列出人们电话号码的电话簿
将您的所有数据保存在一个大的 blob 中更简单、更明智,也更容易在您的计算机缓存中使用.一个包含 N 个向量的向量在操作上要复杂得多(请记住,每个向量都需要动态分配和大小管理操作!);一个向量就是一个向量.您还没有将工作量乘以 N.
Keeping all your data in a single big blob is more simple, more sensible, and easier on your computer's cache. A vector with N vectors inside it is much more operationally complex (remember, each of those requires a dynamic allocation and size management operations!); one vector is, well, one vector. You haven't multiplied the workload by N.
唯一的缺点是要使用一维底层数据存储模拟二维数组访问,您需要编写一个外观.幸运的是,这很容易.
The only downside really is that to simulate 2D array access with a 1D underlying data store, you need to write a facade. Fortunately, this is very easy.
现在是主观部分:总的来说,我会说这是值得的,除非您真的很着急并且您的代码质量并不特别重要.
Now for the subjective part: on balance I'd say that it's worthwhile unless you're really in a rush and your code quality doesn't particularly matter.
这篇关于向量向量有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!