The question of vector vs valarray has already been asked here.
My question refers specifically to the case of C++11. I have been reading a "A Tour of C++" and "The C++ Programming Language". Both books are written by Bjarne Stroustrup. In the first book the author explains that std::valarray
should be preferred for numerical computing (Chapter 12). But then in the second book, in chapter 29, the author implements a Matrix class in terms of a std::vector
.
Also by doing a bit of googling, it seems that performance-wise, a std::vector
is just as fast as dynamically allocated "raw arrays".
So in the context of C++11, which container should be preferred for numerical computing?
My take on this would be that since std::vector
provides fast access to its contents using the operator[]
(which returns a reference to the data with no bounds checking) and that it is also safer to use a std::vector
over a dynamically allocated array, std::vector
should be preferred.
Also, from C++11 onwards:
std::vector
provides direct access to the underlying data usingstd::vector::data()
- std::vector can be resized in order to use less memory using
std::vector::shrink_to_fit()