gpt4 book ai didi

c++ - valarray 与 vector : Why was valarray introduced?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:06:30 25 4
gpt4 key购买 nike

是的,这个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 [...]

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

valarrayvector类模板,因此,它们在实例化之前甚至不存在
当然,一个 vector<int>不会导致别名问题超过 valarray<int>

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

最佳答案

关注点分离? vectorvalarray 解决不同的问题。引用标准,vector 是 (§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 表示一组值。它也不可调整大小,只能分配。

关于c++ - valarray 与 vector : Why was valarray introduced?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12580628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com