public member function
<valarray>

std::valarray::valarray

default (1)
valarray();
size (2)
explicit valarray (size_t n);
fill (3)
valarray (const T& val, size_t n);
array (4)
valarray (const T* p, size_t n);
copy (5)
valarray (const valarray& x);
sub-array (6)
valarray (const slice_array<T>& sub);valarray (const gslice_array<T>& sub);valarray (const mask_array<T>& sub);valarray (const indirect_array<T>& sub);
default (1)
valarray();
size (2)
explicit valarray (size_t n);
fill (3)
valarray (const T& val, size_t n);
array (4)
valarray (const T* p, size_t n);
copy/move (5)
valarray (const valarray& x);valarray (valarray&& x) noexcept;
sub-array (6)
valarray (const slice_array<T>& sub);valarray (const gslice_array<T>& sub);valarray (const mask_array<T>& sub);valarray (const indirect_array<T>& sub);
initializer list (7)
valarray (initializer_list<T> il);
valarray constructor
Constructs a valarray object:
(1) default constructor
Constructs an empty valarray, with no elements. The size can later be modified by calling member resize.
(2) size-initialization
Constructs a valarray with n value-initialized elements.
(3) fill-initialization
Constructs a valarray with n elements, each initialized to val.
(4) array-initialization
Constructs a valarray with n elements, initialized to the values of the elements in the array pointed by p.
(5) copy / move constructor
Constructs a valarray with the contents of x.
The move-constructor is guaranteed to operate on constant time, leaving x in an unspecified but valid state.
(6) sub-array
Constructs a valarray with the contents of a valarray subselection.
(7) initializer list
Constructs a valarray with a copy of each of the elements in il, in the same order.

Parameters

n
Length of array.
size_t is an unsigned integral type.
val
Value to which each of the elements is initialized.
p
Pointer to an array of elements. The first n elements are used as initial values for the elements in the valarray.
x
A valarray object of the same type (with the same class template argument T).
sub
The result of a valarray subscripting operation.
il
An initializer_list object.
These objects are automatically constructed from initializer list declarators.
T is the template argument of valarray (the value type).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// valarray constructor example
#include <iostream>     // std::cout
#include <valarray>     // std::valarray, std::slice

int main ()
{
  int init[]= {10,20,30,40};
  std::valarray<int> first;                             // (empty)
  std::valarray<int> second (5);                        // 0 0 0 0 0
  std::valarray<int> third (10,3);                      // 10 10 10
  std::valarray<int> fourth (init,4);                   // 10 20 30 40
  std::valarray<int> fifth (fourth);                    // 10 20 30 40
  std::valarray<int> sixth (fifth[std::slice(1,2,1)]);  // 20 30

  std::cout << "sixth sums " << sixth.sum() << '\n';

  return 0;
}

Output:

sixth sums 50


Complexity

Constant for default-constructor and move-constructor.
For others, depends on library implementation (operations may be parallelized).

Iterator validity

The move constructor invalidates iterators, references and sub-arrays of x.

Data races

All elements effectively copied are accessed.
The move constructor modifies x.

Exception safety

If any operation performed on the elements throws an exception, it causes undefined behavior.
If the function fails to allocate storage it may throw an exception (such as bad_alloc), although this is not mandated.
The move constructor never throws exceptions (no-throw guarantee).

See also