A valarray can be constructed by any of the following means:
Default constructor. The default constructor is provided to allow arrays of valarrays. Use the resize function to adjust the valarray after construction.
Construction and initialization. A valarray provides two ways to start with a given size and values:
valarray(const T& v,size_t n) constructs a valarray of size n with each element initialized to the value v.
valarray(const T* p,size_t n) constructs a valarray of size n with each element initialized to the corresponding element of the array pointed to by p. This constructor allows a program to transfer data with maximum efficiency from an ordinary `C' array (resulting from a file operation, for example) into a valarray.
Copy constructor. The copy constructor has value semantics.
Conversion constructors. Class valarray provides four conversion constructors for converting from auxiliary classes generated by subscript operations. We'll look at these classes in detail and describe the use of the conversion constructors in Section 22.4.2.
The following example shows the use of the first three categories of constructors:
#include <valarray> std::valarray<int> v1; // construct an empty valarray std::valarray<int> v2(1,3); // construct a valarray of three // elements, all initialized to 1 v1.resize(3,2); // resize the first valarray to three // elements, all initialized to 2 std::valarray<int> v3(v1); // v3 gets a copy of v1's elements.