Library: Numerics
Does not inherit
An optimized array class for numeric operations
#include <valarray> namespace std { template <class T > class valarray; }
Class valarray and its associated classes slice_array, gslice_array, mask_array, and indirect_array, represent and manipulate one-dimensional arrays of values. Elements in a valarray are indexed sequentially beginning with zero.
Unlike other classes in the Standard Library, valarray can only be used with a fairly narrow range of types. This restriction ensures that numeric operations on a valarray can be as efficient as possible by avoiding aliasing ambiguities and excess temporaries.
namespace std { template <class T> class valarray { public: // types typedef T value_type; // constructors valarray( ); explicit valarray(size_t); valarray(const T&, size_t); valarray(const T* , size_t); valarray(const valarray<T>&); valarray(const slice_array<T>&); valarray(const gslice_array<T>&); valarray(const mask_array<T>&); valarray(const indirect_array<T>&); // destructor ~valarray(); // operator = valarray<T>& operator=(const valarray<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>&); valarray<T>& operator=(const T&); // operator[] 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>&); // unary operators valarray<T> operator+() const; valarray<T> operator-() const; valarray<T> operator~() const; valarray<bool> operator!() const; // computed assignment 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>&); 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&); // others 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, const T& = T() ); }; // Nonmember binary operators template<class T> valarray<T> operator*(const valarray<T>&, const valarray<T>&); template<class T> valarray<T> operator/(const valarray<T>&, const valarray<T>&); template<class T> valarray<T> operator%(const valarray<T>&, const valarray<T>&); template<class T> valarray<T> operator+(const valarray<T>& , const valarray<T>&); template<class T> valarray<T> operator-(const valarray<T>& , const valarray<T>&); template<class T> valarray<T> operator^(const valarray<T>&, const valarray<T>&); template<class T> valarray<T> operator&(const valarray<T>&, const valarray<T>&); template<class T> valarray<T> operator|(const valarray<T>&, const valarray<T>&); template<class T> valarray<T> operator<<(const valarray<T>&, const valarray<T>&); template<class T> valarray<T> operator>>(const valarray<T>&, const valarray<T>&); template<class T> valarray<bool> operator&&(const valarray<T>&, const valarray<T>&); template<class T> valarray<T> operator*(const valarray<T>& , const T& ); template<class T> valarray<T> operator/(const valarray<T>& , const T& ); template<class T> valarray<T> operator%(const valarray<T>&, const T&); template<class T> valarray<T> operator+(const valarray<T>& , const T& ); template<class T> valarray<T> operator-(const valarray<T>& , const T& ); template<class T> valarray<T> operator^(const valarray<T>&, const T&); template<class T> valarray<T> operator&(const valarray<T>&, const T&); template<class T> valarray<T> operator|(const valarray<T>&, const T&); template<class T> valarray<T> operator<<(const valarray<T>&, const T&); template<class T> valarray<T> operator>>(const valarray<T>&, const T&); template<class T> valarray<bool> operator&&(const valarray<T>&, const T&); template<class T> valarray<T> operator*(const T& , const valarray<T>& ); template<class T> valarray<T> operator/(const T& , const valarray<T>& ); template<class T> valarray<T> operator%(const T&, const valarray<T>&); template<class T> valarray<T> operator+(const T& , const valarray<T>& ); template<class T> valarray<T> operator-(const T& , const valarray<T>& ); template<class T> valarray<T> operator^(const T&, const valarray<T>&); template<class T> valarray<T> operator&(const T&, const valarray<T>&); template<class T> valarray<T> operator|(const T&, const valarray<T>&); template<class T> valarray<T> operator<<(const T&, const valarray<T>&); template<class T> valarray<T> operator>>(const T&, const valarray<T>&); template<class T> valarray<bool> operator&&(const T&, const valarray<T>&); // Nonmember logical operators template<class T> valarray<bool> operator==(const valarray<T>& , const valarray<T>& ); template<class T> valarray<bool> operator!=(const valarray<T>& , const valarray<T>& ); template<class T> valarray<bool> operator<(const valarray<T>& , const valarray<T>& ); template<class T> valarray<bool> operator>(const valarray<T>& , const valarray<T>& ); template<class T> valarray<bool> operator<=(const valarray<T>& , const valarray<T>& ); template<class T> valarray<bool> operator>=(const valarray<T>& , const valarray<T>& ); template<class T> valarray<bool> operator||(const valarray<T>& , const valarray<T>&); template<class T> valarray<bool> operator==(const valarray<T>& , const T& ); template<class T> valarray<bool> operator!=(const valarray<T>& , const T& ); template<class T> valarray<bool> operator<(const valarray<T>& , const T& ); template<class T> valarray<bool> operator>(const valarray<T>& , const T& ); template<class T> valarray<bool> operator<=(const valarray<T>& , const T& ); template<class T> valarray<bool> operator>=(const valarray<T>& , const T& ); template<class T> valarray<bool> operator||(const valarray<T>& , const T& ); template<class T> valarray<bool> operator==(const T& , const valarray<T>& ); template<class T> valarray<bool> operator!=(const T& , const valarray<T>& ); template<class T> valarray<bool> operator<(const T& , const valarray<T>& ); template<class T> valarray<bool> operator>(const T& , const valarray<T>& ); template<class T> valarray<bool> operator<=(const T& , const valarray<T>& ); template<class T> valarray<bool> operator>=(const T& , const valarray<T>& ); template<class T> valarray<bool> operator||(const T& , const valarray<T>& ); // Nonmember transcendental functions template<class T> valarray<T> abs(const valarray<T>& ); template<class T> valarray<T> acos(const valarray<T>& ); template<class T> valarray<T> asin(const valarray<T>& ); template<class T> valarray<T> atan(const valarray<T>& ); template<class T> valarray<T> cos(const valarray<T>& ); template<class T> valarray<T> cosh(const valarray<T>& ); template<class T> valarray<T> exp(const valarray<T>& ); template<class T> valarray<T> log(const valarray<T>& ); template<class T> valarray<T> log10(const valarray<T>& ); template<class T> valarray<T> sinh(const valarray<T>& ); template<class T> valarray<T> sin(const valarray<T>& ); template<class T> valarray<T> sqrt(const valarray<T>& ); template<class T> valarray<T> tan(const valarray<T>& ); template<class T> valarray<T> tanh(const valarray<T>& ); template<class T> valarray<T> atan2(const valarray<T>& , const valarray<T>& ); template<class T> valarray<T> atan2(const valarray<T>& , const T& ); template<class T> valarray<T> atan2(const T& , const valarray<T>& ); template<class T> valarray<T> pow(const valarray<T>& , const valarray<T>& ); template<class T> valarray<T> pow(const valarray<T>& , const T& ); template<class T> valarray<T> pow(const T& , const valarray<T>& ); }
valarray();
Creates a valarray of length zero.
explicit valarray(size_t n);
Creates a valarray of length n, containing n values initialized with the default value for type T. T must have a default constructor.
valarray(const T& value, size_t n);
Creates a valarray of length n, containing n values initialized with value.
valarray(const T* value, size_t n);
Creates a valarray of length n, containing n values initialized with the first n elements pointed to by value. The array pointed to by value must contain at least n values.
valarray(const valarray<T>& x);
Creates a copy of x.
valarray(const slice_array<T>& x);
Creates a valarray from the slice_array x.
valarray(const gslice_array<T>& x);
Creates a valarray from the gslice_array x.
valarray(const mask_array<T>& x);
Creates a valarray from the mask_array x.
valarray(const indirect_array<T>& x);
Creates a valarray from the indirect_array x.
~valarray();
Applies ~T() to every element in the valarray and returns all allocated memory.
NOTE -- Before calling these functions, portable code should guarantee that both sides of the assignment operator refer to objects of the same size.
valarray<T>& operator=(const valarray<T>& x);
Assigns to each element of self the corresponding value from x. If self has more or fewer elements than x, then the resulting behavior is undefined.
self is resized to match the size of x. Returns a reference to self.
valarray<T>& operator=(const T& x);
Assigns to each element of self the value of x. Returns a reference to self.
valarray<T>& operator=(const slice_array<T>& x);
Copies elements from x into self by stepping through each slice consecutively. If self has more or fewer elements than x, then self is resized to match the size of x. Returns a reference to self.
valarray<T>& operator=(const gslice_array<T>& x);
Copies elements from x into self by stepping through each slice consecutively. If self has more or fewer elements than x, then self is resized to match the size of x. Returns a reference to self.
valarray<T>& operator=(const mask<T>& x);
Copies each consecutive element from x into self. If self has more or fewer elements than x, then self is resized to match the size of x. Returns a reference to self.
valarray<T>& operator=(const indirect_array<T>& x);
Copies each consecutive element from x into self. If self has more or fewer elements than x, then self is resized to match the size of x. Returns a reference to self.
T& operator[](size_type n);
Returns a reference to element n of self. The result can be used as an lvalue. This reference is valid until the resize function is called or the array is destroyed. The index n must be between 0 and size less one.
T operator[](size_type n) const;
Returns the value at element n of self. The index n must be between 0 and size less one.
valarray<T> operator[](slice s) const;
Returns a subset of the self as specified by s. The return value is a new valarray object. See slice for a description of a BLAS-like slice.
slice_array<T> operator[](slice s);
Returns a subset of the self as specified by s. The return value is a slice_array referencing elements inside self. See slice and slice_array.
valarray<T> operator[](const gslice& s) const;
Returns a subset of the self as specified by s. The return value is a new valarray object. See gslice for a description of a generalized slice.
gslice_array<T> operator[](const gslice& s);
Returns a subset of the self as specified by s. The return value is a gslice_array referencing elements inside self. See gslice and gslice_array.
valarray<T> operator[](const valarray<bool>& v) const;
Returns a subset of the self as specified by v. The return value is a new valarray object.
mask_array<T> operator[](const valarray<bool>& v);
Returns a subset of the self as specified by v. The return value is a mask_array referencing elements inside self. See mask_array.
valarray<T> operator[](const valarray<size_t>& v) const;
Returns a subset of the self as specified by v. The return value is a new valarray object.
Indirect_array<T> operator[](const valarray<size_t>& v);
Returns a subset of the self as specified by v. The return value is a indirect_array referencing elements inside self. See indirect_array.
valarray<T> operator+() const;
Returns a new valarray object of the same size as the array in self where each element has been initialized by applying operator+() to the corresponding element in self. This operation can only be applied to a valarray instantiated on a type T that supports an operator+() that returns T or a type convertible to T.
valarray<T> operator-() const;
Returns a new valarray object of the same size as the array in self where each element has been initialized by applying operator-() to the corresponding element in self. This operation can only be applied to a valarray instantiated on a type T that supports an operator-() returning T or a type convertible to T.
valarray<T> operator~() const;
Returns a new valarray object of the same size as the array in self where each element has been initialized by applying operator~() to the corresponding element in self. This operation can only be applied to a valarray instantiated on a type T that supports an operator~() returning T or a type convertible to T.
valarray<bool> operator!() const;
Returns a new valarray object of the same size as the array in self where each element has been initialized by applying operator!() to the corresponding element in self. This operation can only be applied to a valarray instantiated on a type T that supports an operator!() returning bool or a type convertible to bool.
valarray<T>& operator*=(const valarray<T>& val); valarray<T>& operator/=(const valarray<T>& val); valarray<T>& operator%=(const valarray<T>& val); valarray<T>& operator+=(const valarray<T>& val); valarray<T>& operator-=(const valarray<T>& val); valarray<T>& operator^=(const valarray<T>& val); valarray<T>& operator&=(const valarray<T>& val); valarray<T>& operator|=(const valarray<T>& val); valarray<T>& operator<<=(const valarray<T>& val); valarray<T>& operator>>=(const valarray<T>& val);
Applies the indicated operation to each element in self, using the corresponding element from val as the right hand argument (for example, for all 0 <= n < *this.size(), *this[n] += val[n]). This operation can only be applied to a valarray instantiated on a type T that supports the indicated operation. The length of val must also equal the length of self. Returns self.
valarray<T>& operator*=(const T& val); valarray<T>& operator/=(const T& val); valarray<T>& operator%=(const T& val); valarray<T>& operator+=(const T& val); valarray<T>& operator-=(const T& val); valarray<T>& operator^=(const T& val); valarray<T>& operator&=(const T& val); valarray<T>& operator|=(const T& val); valarray<T>& operator<<=(const T& val); valarray<T>& operator>>=(const T& val);
Applies the indicated operation to each element in self, using val as the right hand argument (for example, for all 0 <= n < *this.size(), *this[n] += val). This operation can only be applied to a valarray instantiated on a type T that supports the indicated operation. Returns self.
size_t size() const;
Returns the number of elements.
T sum() const;
This function uses operator+= to sum all the elements of the array. Sum can only be called for a valarray instantiated on a type that supports operator+=(). The array must also have at least one element. Returns the sum of all elements in the array.
T min() const;
This function uses operator<() to find the minimum element in the array. The array must have at least one element. Returns the minimum of all elements in the array.
T max() const;
This function uses operator<() to find the maximum element in the array. The array must have at least one element. Returns the maximum of all elements in the array.
valarray<T> shift(int n) const;
This function returns a new valarray object whose elements have all been shifted n places to left or right with respect to self. A positive value of n shifts the elements to the left, a negative value to the right. The default constructor for T is used to fill in behind the shifting elements. For example, applying shift(2) to an array corresponding to [3,4,5,6] results in [5,6,0,0], and applying shift(-1) to [3,4,5,6] results in [0,3,4,5].
valarray<T> cshift(int n) const;
This function returns a new valarray object whose elements have all been rotated n places to left or right with respect to self. A positive value of n shifts the elements to the left, a negative value to the right. For example, applying shift(2) to an array corresponding to [3,4,5,6] results in [5,6,3,4], and applying shift(-1) to [3,4,5,6] results in [6,3,4,5].
valarray<T> apply(T func(T)) const;
This function returns a new valarray object with the same length as the array in self but whose elements have all been initialized by applying the argument function func to the corresponding element in self (in other words, for all n < *this.size(), the nth element of the returned array equals func(*this[n])).
valarray<T> apply(T func(const T&)) const;
This function returns a new valarray object with the same length as the array in self but whose elements have all been initialized by applying the argument function func to the corresponding element in self (in other words, for all 0 <= n < *this.size(), the nth element of the returned array equals func(*this[n])).
void resize(size_type sz, T c = T());
Changes the length of self to sz, and assigns c to every element. This function also invalidates all outstanding references to self.
template <class T> valarray<T> operator*(const valarray<T>& lhs, const valarray<T>& rhs); template <class T> valarray<T> operator/(const valarray<T>& lhs, const valarray<T>& rhs); template <class T> valarray<T> operator%(const valarray<T>& lhs, const valarray<T>& rhs); template <class T> valarray<T> operator+(const valarray<T>& lhs, const valarray<T>& rhs); template <class T> valarray<T> operator-(const valarray<T>& lhs, const valarray<T>& rhs); template <class T> valarray<T> operator^(const valarray<T>& lhs, const valarray<T>& rhs); template <class T> valarray<T> operator&(const valarray<T>& lhs, const valarray<T>& rhs); template <class T> valarray<T> operator|(const valarray<T>& lhs, const valarray<T>& rhs); template <class T> valarray<T> operator<<(const valarray<T>& lhs, const valarray<T>& rhs); template <class T> valarray<T> operator>>(const valarray<T>& lhs, const valarray<T>& rhs);
Returns a new valarray object of the same size as the argument arrays where each element has been initialized by applying the indicated operation to the corresponding elements in the argument arrays. The operation can only be applied to a valarray instantiated on a type T that supports a form of the indicated operation that returns T or a type convertible to T. The argument arrays must have the same length.
template <class T> valarray<T> operator*(const valarray<T>& lhs, T& rhs); template <class T> valarray<T> operator/(const valarray<T>& lhs, const T& rhs); template <class T> valarray<T> operator%(const valarray<T>& lhs, const T& rhs); template <class T> valarray<T> operator+(const valarray<T>& lhs, const T& rhs); template <class T> valarray<T> operator-(const valarray<T>& lhs, const T& rhs); template <class T> valarray<T> operator^(const valarray<T>& lhs, const T& rhs); template <class T> valarray<T> operator&(const valarray<T>& lhs, const T& rhs); template <class T> valarray<T> operator|(const valarray<T>& lhs, const T& rhs); template <class T> valarray<T> operator<<(const valarray<T>& lhs, const T& rhs); template <class T> valarray<T> operator>>(const valarray<T>& lhs, const T& rhs);
Returns a new valarray object of the same size as the valarray lhs where each element has been initialized by applying the indicated operation to the corresponding element in lhs and the value rhs. The operation can only be used with a type T that supports a form of the indicated operation that returns T or a type convertible to T.
template <class T> valarray<T> operator*(const T& rhs, valarray<T>& rhs); template <class T> valarray<T> operator/(const T& lhs, const valarray<T>& rhs); template <class T> valarray<T> operator%(const T& lhs, const valarray<T>& rhs); template <class T> valarray<T> operator+(const T& lhs, const valarray<T>& rhs); template <class T> valarray<T> operator-(const T& lhs, const valarray<T>& rhs); template <class T> valarray<T> operator^(const T& lhs, const valarray<T>& rhs); template <class T> valarray<T> operator&(const T& lhs, const valarray<T>& rhs); template <class T> valarray<T> operator|(const T& lhs, const valarray<T>& rhs); template <class T> valarray<T> operator<<(const T& lhs, const valarray<T>& rhs); template <class T> valarray<T> operator>>(const T& lhs, const valarray<T>& rhs);
Returns a new valarray object of the same size as the valarray rhs where each element has been initialized by applying the indicated operation to the corresponding element in rhs and the value lhs. The operation can only be used with a type T that supports a form of the indicated operation that returns T or a type convertible to T.
template <class T> valarray<bool> operator==(const valarray<T>& lhs, const valarray<T>& rhs); template <class T> valarray<bool> operator!=(const valarray<T>& lhs, const valarray<T>& rhs); template <class T> valarray<bool> operator<(const valarray<T>& lhs, const valarray<T>& rhs); template <class T> valarray<bool> operator>(const valarray<T>& lhs, const valarray<T>& rhs); template <class T> valarray<bool> operator<=(const valarray<T>& lhs, const valarray<T>& rhs); template <class T> valarray<bool> operator>=(const valarray<T>& lhs, const valarray<T>& rhs); template <class T> valarray<bool> operator&&(const valarray<T>& lhs, const valarray<T>& rhs); template <class T> valarray<bool> operator||(const valarray<T>& lhs, const valarray<T>& rhs);
Returns a valarray<bool> object of the same size as the argument arrays where each element has been initialized by applying the indicated operation to the corresponding elements in the argument arrays. The operation can only be applied to a valarray instantiated on a type T that support a form of the indicated operation that returns bool or a type convertible to bool. The argument arrays must have the same length.
template <class T> valarray<bool> operator==(const valarray<T>& lhs, T& rhs); template <class T> valarray<bool> operator!=(const valarray<T>& lhs, const T& rhs); template <class T> valarray<bool> operator<(const valarray<T>& lhs, const T& rhs); template <class T> valarray<bool> operator>(const valarray<T>& lhs, const T& rhs); template <class T> valarray<bool> operator<=(const valarray<T>& lhs, const T& rhs); template <class T> valarray<bool> operator>=(const valarray<T>& lhs, const T& rhs); template <class T> valarray<bool> operator&&(const valarray<T>& lhs, const T& rhs); template <class T> valarray<bool> operator||(const valarray<T>& lhs, const T& rhs);
Returns a valarray<bool> object of the same size as the valarray lhs where each element has been initialized by applying the indicated operation to the corresponding element in lhs and the value rhs. The operation can only be used with a type T that supports a form of the indicated operation that returns bool or a type convertible to bool.
template <class T> valarray<bool> operator==(const T& rhs, valarray<T>& rhs); template <class T> valarray<bool> operator!=(const T& lhs, const valarray<T>& rhs); template <class T> valarray<bool> operator<(const T& lhs, const valarray<T>& rhs); template <class T> valarray<bool> operator>(const T& lhs, const valarray<T>& rhs); template <class T> valarray<bool> operator<=(const T& lhs, const valarray<T>& rhs); template <class T> valarray<bool> operator>=(const T& lhs, const valarray<T>& rhs); template <class T> valarray<bool> operator&&(const T& lhs, const valarray<T>& rhs); template <class T> valarray<bool> operator||(const T& lhs, const valarray<T>& rhs);
Returns a valarray<bool> object of the same size as the valarray rhs where each element has been initialized by applying indicated operation to the corresponding element in rhs and the value lhs. The operation can only be used with a type T that supports a form of the indicated operation that returns bool or a type convertible to bool.
template <class T> valarray<T> abs(const valarray<T>& v); template <class T> valarray<T> acos(const valarray<T>& v); template <class T> valarray<T> asin(const valarray<T>& v); template <class T> valarray<T> atan(const valarray<T>& v); template <class T> valarray<T> cos(const valarray<T>& v); template <class T> valarray<T> cosh(const valarray<T>& v); template <class T> valarray<T> exp(const valarray<T>& v); template <class T> valarray<T> log(const valarray<T>& v); template <class T> valarray<T> log10(const valarray<T>& v); template <class T> valarray<T> sin(const valarray<T>& v); template <class T> valarray<T> sinh(const valarray<T>& v); template <class T> valarray<T> sqrt(const valarray<T>& v); template <class T> valarray<T> tan(const valarray<T>& v); template <class T> valarray<T> tanh(const valarray<T>& v);
Returns a new valarray object of the same size as the argument array where each element has been initialized by applying the indicated transcendental function to the corresponding elements in the argument array. The operation can only be applied to a valarray instantiated on a type T that supports a unique form of the indicated function that returns T or a type convertible to T.
template <class T> valarray<T> atan2(const valarray<T>& v, const valarray<T>& v2); template <class T> valarray<T> pow(const valarray<T>& v, const valarray<T>& v2);
Returns a new valarray object of the same size as the argument arrays where each element has been initialized by applying the indicated transcendental function to the corresponding elements in the argument arrays. The operation can only be applied to a valarray instantiated on a type T that supports a unique form of the indicated function that returns T or a type convertible to T.
template <class T> valarray<T> atan2(const valarray<T>& v, const T& v2); template <class T> valarray<T> pow(const valarray<T>& v, const T& v2);
Returns a new valarray object of the same size as the argument array v where each element has been initialized by applying the indicated transcendental function to the corresponding elements in v along with the value v2. The operation can only be applied to a valarray instantiated on a type T that supports a unique form of the indicated function that returns T or a type convertible to T.
template <class T> valarray<T> atan2(const T& v, const valarray<T> v2); template <class T> valarray<T> pow(const T& v, const valarray<T> v2);
Returns a new valarray object of the same size as the argument array v2 where each element has been initialized by applying the indicated transcendental function to the corresponding elements in v2 along with the value v. The operation can only be applied to a valarray instantiated on a type T that supports a unique form of the indicated function that returns T or a type convertible to T.
// // valarray.cpp // #include <cstddef> // for size_t #include <iostream> // for cout #include <valarray> // for valarray template <class T> inline std::ostream& operator<< (std::ostream &out, const std::valarray<T> &v) { out << '['; for (std::size_t i = 0; i < v.size (); ++i) { out << v [i]; if (i < v.size () - 1) out << ','; } return out << ']'; } int main () { const int ibuf[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; const int ibuf2[] = { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 }; // create 2 valarrays of ints std::valarray<int> vi (ibuf, sizeof ibuf / sizeof *ibuf); std::valarray<int> vi2 (ibuf2, sizeof ibuf2 / sizeof *ibuf2); // print them out std::cout << vi << '\n' << vi2 << '\n'; vi += vi2; vi2 *= 2; std::valarray<int> vi3 = vi2 % vi; // print them out again std::cout << vi << '\n' << vi2 << '\n' << vi3 << '\n'; return 0; } Program Output:
[0,1,2,3,4,5,6,7,8,9] [10,11,12,13,14,15,16,17,18,19] [10,12,14,16,18,20,22,24,26,28] [20,22,24,26,28,30,32,34,36,38] [0,10,10,10,10,10,10,10,10,10]
slice, gslice, gslice_array, mask_array, indirect_array
ISO/IEC 14882:1998 -- International Standard for Information Systems --Programming Language C++, Section 26.3.2