valarray class look's same to array class, can you please explain me where would I prefer valarray over array or vice versa?
Difference between std::vector and std::array in C++Vector is a sequential container to store elements and not index based. Array stores a fixed-size sequential collection of elements of the same type and it is index based. Vector is dynamic in nature so, size increases with insertion of elements.
std::valarray is the class for representing and manipulating arrays of values. It supports element-wise mathematical operations and various forms of generalized subscript operators, slicing and indirect access.
Vector is better for frequent insertion and deletion, whereas Arrays are much better suited for frequent access of elements scenario. Vector occupies much more memory in exchange for managing storage and growing dynamically, whereas Arrays are a memory-efficient data structure.
An array is a data structure that stores a fixed number of elements (elements should of the same type) in sequential order. A Vector is a sequential-based container. Arrays can be implemented in a static or dynamic way. Vectors can only be implemented dynamically.
valarray was already in C++03, array is new in C++11valarray is variable length, array is not.valarray is designed for numeric computations and provides plenty of operations including +, -, *, cos, sin, etc... array does not.valarray has an interface to retrieve slices of the array (sub arrays), array does not.valarray is a dynamic data structure, whose size can change at runtime and which performs dynamic allocation. array is a static data structure whose size is determined at compile time (and it is also an aggregate).
Don't use valarray, though; just use a vector instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With