valarray class template — Array of values
template<typename T> class valarray { public: typedef T value_type; valarray( ); explicit valarray(size_t); valarray(const T&, size_t); valarray(const T*, size_t); valarray(const valarray&); valarray(const slice_array<T>&); valarray(const gslice_array<T>&); valarray(const mask_array<T>&); valarray(const indirect_array<T>&); ~valarray( ); valarray<T>& operator=(const valarray<T>&); valarray<T>& operator=(const T&); valarray<T>& operator=(const slice_array<T>&); valarray<T>& operator=(const gslice_array<T>&); valarray<T>& operator=(const mask_array<T>&); valarray<T>& operator=(const indirect_array<T>&); T operator[](size_t) const; T& operator[](size_t); valarray<T> operator[](slice) const; slice_array<T> operator[](slice); valarray<T> operator[](const gslice&) const; gslice_array<T> operator[](const gslice&); valarray<T> operator[](const valarray<bool>&) const; mask_array<T> operator[](const valarray<bool>&); valarray<T> operator[](const valarray<size_t>&) const; indirect_array<T> operator[](const valarray<size_t>&); valarray<T> operator+( ) const; valarray<T> operator-( ) const; valarray<T> operator~( ) const; valarray<bool> operator!( ) const; valarray<T>& operator*= (const T&); valarray<T>& operator/= (const T&); valarray<T>& operator%= (const T&); valarray<T>& operator+= (const T&); valarray<T>& operator-= (const T&); valarray<T>& operator^= (const T&); valarray<T>& operator&= (const T&); valarray<T>& operator|= (const T&); valarray<T>& operator<<=(const T&); valarray<T>& operator>>=(const T&); valarray<T>& operator*= (const valarray<T>&); valarray<T>& operator/= (const valarray<T>&); valarray<T>& operator%= (const valarray<T>&); valarray<T>& operator+= (const valarray<T>&); valarray<T>& operator-= (const valarray<T>&); valarray<T>& operator^= (const valarray<T>&); valarray<T>& operator|= (const valarray<T>&); valarray<T>& operator&= (const valarray<T>&); valarray<T>& operator<<=(const valarray<T>&); valarray<T>& operator>>=(const valarray<T>&); size_t size( ) const; T sum( ) const; T min( ) const; T max( ) const; valarray<T> shift (int) const; valarray<T> cshift(int) const; valarray<T> apply(T func(T)) const; valarray<T> apply(T func(const T&)) const; void resize(size_t sz, T c = T( )); };
The valarray
class template
represents an array of numeric values, with restrictions that permit
an implementation to optimize performance. In particular, an object
cannot be an element of more than one array. A subset, such as a
generalized slice or indirect array, cannot specify a single element
more than once, or else the behavior is undefined.
You can instantiate valarray
with any numerical type as its
template parameter if you limit yourself to using only operations
that are defined for that type. For example, you cannot use operator<<
on valarray<double>
because you cannot
use operator<<
on scalars
of type double
.
Examples of using valarray
can be found throughout this section.
The following are the members of valarray
:
valarray
( )
Constructs an empty valarray
.
explicit
valarray
(size_t n)
Constructs a valarray
of length n
, in which all
elements are initialized to T(
)
.
valarray
(const T& x, size_t n)
Construct a valarray
that contains n
copies of
x
.
valarray
(const T* x, size_t n)
Constructs a valarray
by copying n
elements from
the array x
.
valarray
(const valarray& a)
Constructs a valarray
by copying a
.
valarray
(const slice_array<T>&
a)
, valarray
(const gslice_array<T>&
a)
, valarray
(const mask_array<T>&
a)
, valarray
(const indirect_array<T>&
a)
Constructs a valarray
by copying the elements referenced by a
.
valarray<T>
apply
(T func(T))
const
, valarray<T>
apply
(T func(const T&))
const
Returns a new array whose contents are the result of
calling func
for each
element of the original array.
valarray<T>
cshift
(int n)
const
Performs a circular shift (rotation) by n
places. The return value is a new
array that has the same size as the original, but the element
at new index i
comes from
(i
+ n
) % size(
)
in the original.
T
max
( )
const
Returns the largest element of the array or an undefined
value if the array is empty. Elements are compared using
operator<
.
T
min
( )
const
Returns the smallest element of the array or an
undefined value if the array is empty. Elements are compared
using operator<
.
valarray<T>&
operator=
(const
valarray<T>& a)
Each element of this array is assigned the corresponding
elements of a
. If size( )
!=
a.size(
)
, the behavior is undefined.
valarray<T>&
operator=
(const T& x)
Each element of this array is assigned the scalar value
x
.
valarray<T>&
operator=
(const slice_array<T>&
a)
, valarray<T>&
operator=
(const gslice_array<T>&
a)
, valarray<T>&
operator=
(const mask_array<T>&
a)
, valarray<T>&
operator=
(const indirect_array<T>&
a)
Each element of this array is assigned the corresponding
element from the subset array a
. The value being assigned to an
element must not depend on any other value in this array, that
is, a
cannot be a subset of
*this
, or, if it is, the
element assigned to index i
must
depend only on index i
and not on
values at any other index.
T
operator[]
(size_t i)
const
, T&
operator[]
(size_t i)
Returns the element at index i
. The behavior is undefined for
i
>=
size(
)
. The anti-aliasing rule means that for any two
distinct valarray
objects
a
and b
, and for all valid indices
i
and j
,
you can safely assume that the following is true: &a[
i
]
!=
&b[
j
]
. All values in a single
valarray
object are stored
contiguously, just as in an ordinary array. References become
invalid after a call to resize
.
valarray<T>
operator[]
(slice) const
, slice_array<T>
operator[]
(slice)
, valarray<T>
operator[]
(const gslice&) const
, gslice_array<T>
operator[]
(const gslice&)
, valarray<T>
operator[]
(const valarray<bool>&)
const
, mask_array<T>
operator[]
(const
valarray<bool>&)
, valarray<T>
operator[]
(const valarray<size_t>&)
const
, indirect_array<T>
operator[]
(const
valarray<size_t>&)
Returns a subset of this valarray
. A subset of a const
valarray
is a new valarray
. A subset of a non-const
valarray
is a helper object that
maintains a reference to the original valarray
. (See the descriptions of
the helper classes earlier in this section for details.)
Briefly, the four kinds of subsets are:
A slice
object
specifies a simple slice, suitable for extracting a row or
column of a 2-D array.
A gslice
is a
generalized slice, which permits multidimensional
matrices.
A mask_array
is
created by specifying an array of bool
as the argument. If an
element is true
, that
element is part of the resulting subset.
An indirect_array
is created by specifying an array of size_t
indices as the argument.
Each element specifies an index, and the element at that
index is added to the resulting array.
valarray<T>
operator+
( )
const
, valarray<T>
operator-
( ) const
, valarray<T>
operator~
( )
const
, valarray<bool>
operator!
( )
const
Returns a new valarray
in which each new element
is the result of applying the unary operator to the
corresponding element in the original array.
valarray<T>&
operator*=
(const T&
x)
, valarray<T>&
operator/=
(const T&
x)
, valarray<T>&
operator%=
(const T&
x)
, valarray<T>&
operator+=
(const T&
x)
, valarray<T>&
operator-=
(const T& x)
, valarray<T>&
operator^=
(const T&
x)
, valarray<T>&
operator&=
(const T&
x)
, valarray<T>&
operator|=
(const T&
x)
, valarray<T>&
operator<<=
(const T&
x)
, valarray<T>&
operator>>=
(const T&
x)
Modifies this valarray
by applying the assignment
operator to each element of the array. The return value is
*this
.
valarray<T>&
operator*=
(const
valarray<T>& a)
, valarray<T>&
operator/=
(const
valarray<T>& a)
, valarray<T>&
operator%=
(const
valarray<T>& a)
, valarray<T>&
operator+=
(const
valarray<T>& a)
, valarray<T>&
operator-=
(const
valarray<T>& a)
, valarray<T>&
operator^=
(const
valarray<T>& a)
, valarray<T>&
operator|=
(const
valarray<T>& a)
, valarray<T>&
operator&=
(const
valarray<T>& a)
, valarray<T>&
operator<<=
(const
valarray<T>& a)
, valarray<T>&
operator>>=
(const
valarray<T>& a)
Modifies this valarray
by applying the assignment
operator to each element of the array and to the corresponding
element of a
. The behavior
is undefined if size( )
!=
a.size( )
. The return value is
*this
.
void
resize
(size_t sz, T x = T(
))
Changes the size of the array to sz
and reinitializes every element
of the array to x
.
valarray<T>
shift
(int n)
const
Performs a shift by n
places. The return value is a new array with the same size as
the original array, but the element at index
i
in the new array comes from index
i
+ n
in the original array. If
i
+ n
< 0 or ≥ size( )
, the new element is set to
T( )
.
size_t
size
( )
const
Returns the number of elements in the array.
T
sum
( )
const
Returns the sum of all the elements in the array using
operator+=
. If the array is
empty, the value is undefined.