简体   繁体   English

std :: valarray和std :: array之间有什么区别

[英]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专为数值计算而设计,并提供大量操作,包括+-*cossin等... 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.

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