C ++ 11: should valarray or vector be used for numerical calculations

The question about the vs valarray vector is already asked here. My question is specific to the case of C ++ 11. I read "A Tour of C ++" and "C ++ Programming Language" . Both books are written by Bjar Straustup. In the first book, the author explains that it is preferable for numerical calculations std::valarray(chapter 12). But then in the second book, in chapter 29, the author implements the Matrix class in terms of a std::vector.

In addition, having done a bit of googling , it seems that performance std::vectoris as fast as dynamically distributed "raw arrays".

So, in the context of C ++ 11, which container should be preferred for numerical computing?

My assumption is that since it std::vectorprovides quick access to its contents with operator[](which returns a link to data without border checks), and that it is also safer to use std::vectorover a dynamically allocated array, it std::vectorshould be preferable. Also, starting with C ++ 11:

  • std::vector provides direct access to basic data with std::vector::data()
  • std :: vector can be resized to use less memory using std::vector::shrink_to_fit()
+6
source share
1 answer

valarray , , . , , do v3 = sin(v2 + v1*3)

, , , Eigen

+2

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


All Articles