valarray与vector:为什么引入valarray? [英] valarray vs. vector: Why was valarray introduced?

查看:89
本文介绍了valarray与vector:为什么引入valarray?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,之前曾有人问过这个 ,答案是:

Yes, this has been asked before, and the answer has been:

valarray s(值数组)旨在将Fortran的某些速度引入C ++.您不会制作指针的 valarray ,因此编译器可以对代码进行假设并更好地对其进行优化.(Fortran这么快的主要原因是没有指针类型,所以不能有指针别名.)

valarrays (value arrays) are intended to bring some of the speed of Fortran to C++. You wouldn't make a valarray 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆