微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/c/116719.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐