Valarray vs. vector: Why was valarray introduced?

Yes, this one was asked before , and the answer was:

valarray (arrays of values) are designed to port some Fortran speed to C ++. You would not make valarray pointers so that the compiler can make assumptions about the code and optimize it better. (The main reason Fortran is so fast is because there is no pointer, so there can be no smoothing of the pointer.)

or

valarray should also eliminate any possibility of smoothing [...]

But these answers make no sense to me.

valarray and vector are class templates and, as such, they do not even exist until instantiation.
And, of course, vector<int> does not cause problems with an alias more than valarray<int> does.

Given this, what was the purpose of valarray , and why didn't they just add the same functionality to vector ?

+6
source share
2 answers

Separation of anxiety? A vector and a valarray solve different problems. Quoting from the standard, a vector is ( §23.3.6.1 [vector.overview] p1 )

... a sequence container that supports random access iterators. In addition, it maintains (depreciates) a constant insertion and erasure time of operations at the end; insert and wash in the middle, take linear time. Storage management is handled automatically, although you can use hints to increase efficiency.

whereas a valarray is ( §26.6.2.1 [template.valarray.overview] p1 )

... a one-dimensional intelligent array with elements sequentially numbered from zero. This is a representation of the mathematical concept of an ordered set of values. A higher dimensional illusion can be expressed by the familiar idiom of computed indices, along with the powerful subset capabilities provided by generalized indices.

As you can see, they serve different purposes. A vector is a generic dynamic array, and valarray is a collection of values. It also cannot be changed and is only assigned.

+11
source
  • valarray has a slice mechanism

  • Valarray is expected to be implemented using an expression template for its numerical operators

+5
source

Source: https://habr.com/ru/post/926195/


All Articles