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
is also supposed to eliminate any possibility of aliasing […]
但这些答案对我来说毫无意义.
valarray和vector是类模板,因此,它们在实例化之前甚至不存在.
当然,矢量< int>除了valarray< int>之外不会引起别名问题确实.
鉴于此,valarray的目的是什么,为什么他们不是简单地将相同的功能放入向量中呢?
解决方法
… 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.
如您所见,它们用于不同的目的.向量是广义动态数组,而valarray表示一组值.它也不可调整,只能分配.