[英]What is the difference between std::valarray and std::array
valarray
类看起来与array
类相同,你能解释一下我在哪里更喜欢valarray
不是array
,反之亦然?
valarray
was already in C++03, array
is new in C++11 valarray
已经在C ++ 03中, array
是C ++ 11中的新功能 valarray
is variable length, array
is not. valarray
是可变长度, array
不是。 valarray
is designed for numeric computations and provides plenty of operations including +
, -
, *
, cos
, sin
, etc... array
does not. valarray
专为数值计算而设计,并提供大量操作,包括+
, -
, *
, cos
, sin
等... array
不会。 valarray
has an interface to retrieve slices of the array (sub arrays), array
does not. valarray
有一个接口来检索数组的切片(子数组), array
没有。 The class templates related to std::valarray<T>
are intended to support optimizations techniques known as expression templates . 与
std::valarray<T>
相关的类模板旨在支持称为表达式模板的优化技术。 I haven't tried to do this but my understanding is that the specification doesn't quite require this and also doesn't really support this sufficiently. 我没有尝试这样做,但我的理解是规范并不是非常需要这个并且也没有真正支持这一点。 In general
std::valarray<T>
is a fairly specialized class and it isn't really broadly used. 一般来说,
std::valarray<T>
是一个相当专业的类,并没有真正广泛使用。 Also, I think the template arguments support for std::valarray<T>
are a limited set (eg the numeric built-in types). 另外,我认为
std::valarray<T>
的模板参数支持是一个有限集(例如数字内置类型)。
On the other std::array<T, n>
is a fixed size array supporting, as far as possible while being fixed size, the normal container interface. 在另一个
std::array<T, n>
是一个固定大小的数组,尽可能支持固定大小的普通容器接口。 Essentially, std::array<T>
is a more convenient to use version of T[n]
. 本质上,
std::array<T>
是一个更方便使用的T[n]
版本。
valarray
is a dynamic data structure, whose size can change at runtime and which performs dynamic allocation. valarray
是一种动态数据结构,其大小可以在运行时更改,并执行动态分配。 array
is a static data structure whose size is determined at compile time (and it is also an aggregate). array
是一个静态数据结构,其大小在编译时确定(它也是一个聚合)。
Don't use valarray
, though; 但是,不要使用
valarray
; just use a vector
instead. 只需使用
vector
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.