valarray vs. vector: Why was valarray introduced?

前端 未结 2 693
北荒
北荒 2021-01-18 09:59

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

valarrays (value arrays) are intended to bring some of the speed of Fortran to

相关标签:
2条回答
  • 2021-01-18 10:50

    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.

    while a valarray is a (§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.

    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.

    0 讨论(0)
  • 2021-01-18 11:01
    • valarray has the slice mechanism

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

    0 讨论(0)
提交回复
热议问题