valarray与vector:为什么引入valarray? [英] valarray vs. vector: Why was valarray introduced?
问题描述
Yes, this has been asked before, and the answer has been:
valarray
s(值数组)旨在将Fortran的某些速度引入C ++.您不会制作指针的valarray
,因此编译器可以对代码进行假设并更好地对其进行优化.(Fortran这么快的主要原因是没有指针类型,所以不能有指针别名.)
valarray
s (value arrays) are intended to bring some of the speed of Fortran to C++. You wouldn't make avalarray
of pointers so the compiler can make assumptions about the code and optimise it better. (The main reason that Fortran is so fast is that there is no pointer type so there can be no pointer aliasing.)
或:
valarray
还应该消除别名[...]
valarray
is also supposed to eliminate any possibility of aliasing [...]
但是这些答案对我来说毫无意义.
But these answers make no sense to me.
valarray
和 vector
是类模板,因此,它们甚至在实例化之前都不存在..
当然, vector< int>
不会比 valarray< int>
引起的混叠问题更多.
valarray
and vector
are class templates, and as such, they don't even exist until instantiated.
And of course, a vector<int>
doesn't cause aliasing issues any more than valarray<int>
does.
鉴于此, valarray
的目的是什么,为什么他们不将相同的功能简单地放入 vector
中?
Given this, what was the purpose of valarray
, and why did they not simply put the same functionality into vector
instead?
推荐答案
关注的分离? vector
和 valarray
解决了不同的问题.按照标准引用, vector
是(§23.3.6.1[vector.overview] p1
)
Separation of concern? A vector
and a valarray
solve different problems. Quoting from the standard, a vector
is a (§23.3.6.1 [vector.overview] p1
)
...支持随机访问迭代器的序列容器.此外,它在末尾支持(摊销)恒定时间插入和擦除操作.在中间插入和擦除需要线性时间.存储管理是自动处理的,尽管可以提供一些提示以提高效率.
... sequence container that supports random access iterators. In addition, it supports (amortized) constant time insert and erase operations at the end; insert and erase in the middle take linear time. Storage management is handled automatically, though hints can be given to improve efficiency.
而 valarray
是(§26.6.2.1[template.valarray.overview] p1
)
...一维智能数组,元素从零开始顺序编号.它表示值的有序集合的数学概念.更高维度的错觉可能是由熟悉的计算索引习惯用法以及广义下标运算符提供的强大子集功能引起的
... one-dimensional smart array, with elements numbered sequentially from zero. It is a representation of the mathematical concept of an ordered set of values. The illusion of higher dimensionality may be produced by the familiar idiom of computed indices, together with the powerful subsetting capabilities provided by the generalized subscript operators.
如您所见,它们有不同的用途. vector
是广义的动态数组,而 valarray
表示一组值.它也不是可调整大小的,只能分配.
As you can see, they serve different purposes. A vector
is a generalized dynamic array, while a valarray
represents a set of values. It's also not resizeable and only assignable.
这篇关于valarray与vector:为什么引入valarray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!