c – valarray vs. vector:为什么要引入valarray?

前端之家收集整理的这篇文章主要介绍了c – valarray vs. vector:为什么要引入valarray?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是的,这个 has been asked before,答案是:

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 is also supposed to eliminate any possibility of aliasing […]

但这些答案对我来说毫无意义.

valarray和vector是类模板,因此,它们在实例化之前甚至不存在.
当然,矢量< int>除了valarray< int>之外不会引起别名问题确实.

鉴于此,valarray的目的是什么,为什么他们不是简单地将相同的功能放入向量中呢?

解决方法

分离关注?矢量和valarray解决了不同的问题.引用标准,向量是(§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.

如您所见,它们用于不同的目的.向量是广义动态数组,而valarray表示一组值.它也不可调整,只能分配.

原文链接:https://www.f2er.com/c/116719.html

猜你在找的C&C++相关文章