# <valarray> <valarray> # `` * * * **`gslice· gslice_array· indirect_array· mask_array· slice· slice_array· valarray· valarray`** **`abs· acos· asin· atan· atan2· cos· cosh· exp· log· log10· operator!=· operator%· operator&· operator&&· operator>· operator>>· operator>=· operator<· operator<<· operator<=· operator*· operator+· operator-· operator/· operator==· operator^· operator|· operator||· pow· sin· sinh· sqrt· tan· tanh`** * * * Include the standard header **``** to define the template class `valarray` and numerous supporting template classes and functions. These template classes and functions are permitted unusual latitude, in the interest of improved performance. Specifically, any function described here as returning `valarray` may actually return an object of some other type `Other`. In that case, any other function described here that accepts one or more arguments of type `valarray` must have overloads that accept arbitrary combinations of those arguments, each replaced with an argument of type `Other`. (Put simply, the only way you can detect such a substitution is to go looking for it.) namespace std { class slice; class gslice; // TEMPLATE CLASSES template class valarray; template class slice_array; template class gslice_array; template class mask_array; template class indirect_array; // TEMPLATE FUNCTIONS template valarray operator*(const valarray& left, const valarray& right); template valarray operator*(const valarray left, const Ty& right); template valarray operator*(const Ty& left, const valarray& right); template valarray operator/(const valarray& left, const valarray& right); template valarray operator/(const valarray left, const Ty& right); template valarray operator/(const Ty& left, const valarray& right); template valarray operator%(const valarray& left, const vararray& right); template valarray operator%(const valarray left, const Ty& right); template valarray operator%(const Ty& left, const valarray& right); template valarray operator+(const valarray& left, const valarray& right); template valarray operator+(const valarray left, const Ty& right); template valarray operator+(const Ty& left, const valarray& right); template valarray operator-(const valarray& left, const valarray& right); template valarray operator-(const valarray left, const Ty& right); template valarray operator-(const Ty& left, const valarray& right); template valarray operator^(const valarray& left, const valarray& right); template valarray operator^(const valarray left, const Ty& right); template valarray operator^(const Ty& left, const valarray& right); template valarray operator&(const valarray& left, const valarray& right); template valarray operator&(const valarray left, const Ty& right); template valarray operator&(const Ty& left, const valarray& right); template valarray operator|(const valarray& left, const valarray& right); template valarray operator|(const valarray left, const Ty& right); template valarray operator|(const Ty& left, const valarray& right);Copy to clipboard template valarray operator<<(const valarray& left, const valarray& right); template valarray operator<<(const valarray left, const Ty& right); template valarray operator<<(const Ty& left, const valarray& right); template valarray operator>>(const valarray& left, const valarray& right); template valarray operator>>(const valarray left, const Ty& right); template valarray operator>>(const Ty& left, const valarray& right); template valarray operator&&(const valarray& left, const valarray& right); template valarray operator&&(const valarray left, const Ty& right); template valarray operator&&(const Ty& left, const valarray& right); template valarray operator||(const valarray& left, const valarray& right); template valarray operator||(const valarray left, const Ty& right); template valarray operator||(const Ty& left, const valarray& right);Copy to clipboard template valarray operator==(const valarray& left, const valarray& right); template valarray operator==(const valarray left, const Ty& right); template valarray operator==(const Ty& left, const valarray& right); template valarray operator!=(const valarray& left, const valarray& right); template valarray operator!=(const valarray left, const Ty& right); template valarray operator!=(const Ty& left, const valarray& right); template valarray operator<(const valarray& left, const valarray& right); template valarray operator<(const valarray left, const Ty& right); template valarray operator<(const Ty& left, const valarray& right); template valarray operator>=(const valarray& left, const valarray& right); template valarray operator>=(const valarray left, const Ty& right); template valarray operator>=(const Ty& left, const valarray& right); template valarray operator>(const valarray& left, const valarray& right); template valarray operator>(const valarray left, const Ty& right); template valarray operator>(const Ty& left, const valarray& right); template valarray operator<=(const valarray& left, const valarray& right); template valarray operator<=(const valarray left, const Ty& right); template valarray operator<=(const Ty& left, const valarray& right);Copy to clipboard template valarray abs(const valarray& left); template valarray acos(const valarray& left); template valarray asin(const valarray& left); template valarray atan(const valarray& left); template valarray atan2(const valarray& left, const valarray& right); template valarray atan2(const valarray left, const Ty& right); template valarray atan2(const Ty& left, const valarray& right); template valarray cos(const valarray& left); template valarray cosh(const valarray& left); template valarray exp(const valarray& left); template valarray log(const valarray& left); template valarray&tt;Ty> log10(const valarray& left); template valarray pow(const valarray& left, const valarray& right); template valarray pow(const valarray left, const Ty& right); template valarray pow(const Ty& left, const valarray& right); template valarray sin(const valarray& left); template valarray sinh(const valarray& left); template valarray sqrt(const valarray& left); template valarray tan(const valarray& left); template valarray tanh(const valarray& left); } // namespace stdCopy to clipboard ## `abs` template valarray abs(const valarray& left);Copy to clipboard The template function returns an object of class `valarray`, each of whose elements `I` is the absolute value of `left[I]`. ## `acos` template valarray acos(const valarray& left);Copy to clipboard The template function returns an object of class `valarray`, each of whose elements `I` is the arccosine of `left[I]`. ## `asin` template valarray asin(const valarray& left);Copy to clipboard The template function returns an object of class `valarray`, each of whose elements `I` is the arcsine of `left[I]`. ## `atan` template valarray atan(const valarray& left);Copy to clipboard The template function returns an object of class `valarray`, each of whose elements `I` is the arctangent of `left[I]`. ## `atan2` template valarray atan2(const valarray& left, const valarray& right); template valarray atan2(const valarray left, const Ty& right); template valarray atan2(const Ty& left, const valarray& right);Copy to clipboard The first template function returns an object of class `valarray`, each of whose elements `I` is the arctangent of `left[I] / right[I]`. The second template function stores in element `I` the arctangent of `left[I] / right`. The third template function stores in element `I` the arctangent of `left / right[I]`. ## `cos` template valarray cos(const valarray& left);Copy to clipboard The template function returns an object of class `valarray`, each of whose elements `I` is the cosine of `left[I]`. ## `cosh` template valarray cosh(const valarray& left);Copy to clipboard The template function returns an object of class `valarray`, each of whose elements `I` is the hyperbolic cosine of `left[I]`. ## `exp` template valarray exp(const valarray& left);Copy to clipboard The template function returns an object of class `valarray`, each of whose elements `I` is the exponential of `left[I]`. ## `gslice` class gslice { public: gslice(); gslice(size_t off, const valarray lenarr, const valarray incarr); size_t start() const; const valarray size() const; const valarray stride() const; };Copy to clipboard The class stores the parameters that characterize a `gslice_array` when an object of class `gslice` appears as a subscript for an object of class `valarray`. The stored values include: - a **starting index** - a **length vector** of class `valarray` - a **stride vector** of class `valarray` The two vectors must have the same length. ### `gslice::gslice` gslice(); gslice(size_t off, const valarray lenarr, const valarray incarr);Copy to clipboard The default constructor stores zero for the starting index, and zero-length vectors for the length and stride vectors. The second constructor stores `off` for the starting index, `lenarr` for the length vector, and `incarr` for the stride vector. ### `gslice::size` const valarray size() const;Copy to clipboard The member function returns the stored length vector. ### `gslice::start` size_t start() const;Copy to clipboard The member function returns the stored starting index. ### `gslice::stride` const valarray stride() const;Copy to clipboard The member function returns the stored stride vector. ## `gslice_array` template class gslice_array { public: typedef Ty value_type; void operator=(const valarray right) const; void operator*=(const valarray right) const; void operator/=(const valarray right) const; void operator%=(const valarray right) const; void operator+=(const valarray right) const; void operator-=(const valarray right) const; void operator^=(const valarray right) const; void operator&=(const valarray right) const; void operator|=(const valarray right) const; void operator<<=(const valarray right) const; void operator>>=(const valarray right) const; private: void gslice_array(); // not defined void gslice_array( const gslice_array&); // not defined gslice_array& operator=( const gslice_array&); // not defined };Copy to clipboard The class describes an object that stores a reference to an object `va` of class `valarray`, along with an object `gs` of class `gslice` which describes the sequence of elements to select from the `valarray` object. You construct a `gslice_array` object only by writing an expression of the form `va[gs]`. The member functions of class `gslice_array` then behave like the corresponding function signatures defined for `valarray`, except that only the sequence of selected elements is affected. The sequence is determined as follows. For a length vector `gs.size()` of length `N`, construct the index vector `valarray idx(0, N)`. This designates the initial element of the sequence, whose index `K` within `va` is given by the mapping: K = gs.start(); for (size_t I = 0; I < N; ++I) K += idx[I] * gs.stride()[I];Copy to clipboard The successor to an index vector value is given by: for (size_t I = N; 0 < I--; ) if (++idx[I] < gs.size()[I]) break; else idx[I] = 0;Copy to clipboard For example: const size_t lv[] = {2, 3}; const size_t dv[] = {7, 2}; const valarray len(lv, 2), str(dv, 2); // va[gslice(3, len, str)] selects elements with // indices 3, 5, 7, 10, 12, 14Copy to clipboard ## `indirect_array` template class indirect_array { public: typedef Ty value_type; void operator=(const valarray right) const; void operator*=(const valarray right) const; void operator/=(const valarray right) const; void operator%=(const valarray right) const; void operator+=(const valarray right) const; void operator-=(const valarray right) const; void operator^=(const valarray right) const; void operator&=(const valarray right) const; void operator|=(const valarray right) const; void operator<<=(const valarray right) const; void operator>>=(const valarray right) const; private: private: void indirect_array(); // not defined void indirect_array( const indirect_array&); // not defined indirect_array& operator=( const indirect_array&); // not defined };Copy to clipboard The class describes an object that stores a reference to an object `va` of class `valarray`, along with an object `xa` of class `valarray` which describes the sequence of elements to select from the `valarray` object. You construct an `indirect_array` object only by writing an expression of the form `va[xa]`. The member functions of class `indirect_array` then behave like the corresponding function signatures defined for `valarray`, except that only the sequence of selected elements is affected. The sequence consists of `xa.size()` elements, where element `I` becomes the index `xa[I]` within `va`. For example: const size_t vi[] = {7, 5, 2, 3, 8}; // va[valarray(vi, 5)] selects elements with // indices 7, 5, 2, 3, 8Copy to clipboard ## `log` template valarray log(const valarray& left);Copy to clipboard The template function returns an object of class `valarray`, each of whose elements `I` is the natural logarithm of `left[I]`. ## `log10` template valarray log10(const valarray& left);Copy to clipboard The template function returns an object of class `valarray`, each of whose elements `I` is the base-10 logarithm of `left[I]`. ## `mask_array` template class mask_array { public: typedef Ty value_type; void operator=(const valarray right) const; void operator*=(const valarray right) const; void operator/=(const valarray right) const; void operator%=(const valarray right) const; void operator+=(const valarray right) const; void operator-=(const valarray right) const; void operator^=(const valarray right) const; void operator&=(const valarray right) const; void operator|=(const valarray right) const; void operator<<=(const valarray right) const; void operator>>=(const valarray right) const; private: void mask_array(); // not defined void mask_array( const mask_array&); // not defined gslice_array& operator=( const mask_array&); // not defined };Copy to clipboard The class describes an object that stores a reference to an object `va` of class `valarray`, along with an object `ba` of class `valarray` which describes the sequence of elements to select from the `valarray` object. You construct a `mask_array` object only by writing an expression of the form `va[ba]`. The member functions of class `mask_array` then behave like the corresponding function signatures defined for `valarray`, except that only the sequence of selected elements is affected. The sequence consists of at most `ba.size()` elements. An element `J` is included only if `ba[J]` is true. Thus, there are as many elements in the sequence as there are true elements in `ba`. If `I` is the index of the lowest true element in `ba`, then `va[I]` is element zero in the selected sequence. For example: const bool vb[] = {false, false, true, true, false, true}; // va[valarray(vb, 56] selects elements with // indices 2, 3, 5Copy to clipboard ## `operator!=` template valarray operator!=(const valarray& left, const valarray& right); template valarray operator!=(const valarray left, const Ty& right); template valarray operator!=(const Ty& left, const valarray& right);Copy to clipboard The first template operator returns an object of class `valarray`, each of whose elements `I` is `left[I] != right[I]`. The second template operator stores in element `I` `left[I] != right`. The third template operator stores in element `I` `left != right[I]`. ## `operator%` template valarray operator%(const valarray& left, const valarray& right); template valarray operator%(const valarray left, const Ty& right); template valarray operator%(const Ty& left, const valarray& right);Copy to clipboard The first template operator returns an object of class `valarray`, each of whose elements `I` is `left[I] % right[I]`. The second template operator stores in element `I` `left[I] % right`. The third template operator stores in element `I` `left % right[I]`. ## `operator&` template valarray operator&(const valarray& left, const valarray& right); template valarray operator&(const valarray left, const Ty& right); template valarray operator&(const Ty& left, const valarray& right);Copy to clipboard The first template operator returns an object of class `valarray`, each of whose elements `I` is `left[I] & right[I]`. The second template operator stores in element `I` `left[I] & right`. The third template operator stores in element `I` `left & right[I]`. ## `operator&&` template valarray operator&&(const valarray& left, const valarray& right); template valarray operator&&(const valarray left, const Ty& right); template valarray operator&&(const Ty& left, const valarray& right);Copy to clipboard The first template operator returns an object of class `valarray`, each of whose elements `I` is `left[I] && right[I]`. The second template operator stores in element `I` `left[I] && right`. The third template operator stores in element `I` `left && right[I]`. ## `operator>` template valarray operator>(const valarray& left, const valarray& right); template valarray operator>(const valarray left, const Ty& right); template valarray operator>(const Ty& left, const valarray& right);Copy to clipboard The first template operator returns an object of class `valarray`, each of whose elements `I` is `left[I] > right[I]`. The second template operator stores in element `I` `left[I] > right`. The third template operator stores in element `I` `left > right[I]`. ## `operator>>` template valarray operator>>(const valarray& left, const valarray& right); template valarray operator>>(const valarray left, const Ty& right); template valarray operator>>(const Ty& left, const valarray& right);Copy to clipboard The first template operator returns an object of class `valarray`, each of whose elements `I` is `left[I] >> right[I]`. The second template operator stores in element `I` `left[I] >> right`. The third template operator stores in element `I` `left >> right[I]`. ## `operator>=` template valarray operator>=(const valarray& left, const valarray& right); template valarray operator>=(const valarray left, const Ty& right); template valarray operator>=(const Ty& left, const valarray& right);Copy to clipboard The first template operator returns an object of class `valarray`, each of whose elements `I` is `left[I] >= right[I]`. The second template operator stores in element `I` `left[I] >= right`. The third template operator stores in element `I` `left >= right[I]`. ## `operator<` template valarray operator<(const valarray& left, const valarray& right); template valarray operator<(const valarray left, const Ty& right); template valarray operator<(const Ty& left, const valarray& right);Copy to clipboard The first template operator returns an object of class `valarray`, each of whose elements `I` is `left[I] < right[I]`. The second template operator stores in element `I` `left[I] < right`. The third template operator stores in element `I` `left < right[I]`. ## `operator<<` template valarray operator<<(const valarray& left, const valarray& right); template valarray operator<<(const valarray left, const Ty& right); template valarray operator<<(const Ty& left, const valarray& right);Copy to clipboard The first template operator returns an object of class `valarray`, each of whose elements `I` is `left[I] << right[I]`. The second template operator stores in element `I` `left[I] << right`. The third template operator stores in element `I` `left << right[I]`. ## `operator<=` template valarray operator<=(const valarray& left, const valarray& right); template valarray operator<=(const valarray left, const Ty& right); template valarray operator<=(const Ty& left, const valarray& right);Copy to clipboard The first template operator retrrns an object of class `valarray`, each of whose elements `I` is `left[I] <= right[I]`. The second template operator stores in element `I` `left[I] <= right`. The third template operator stores in element `I` `left <= right[I]`. ## `operator*` template valarray operator*(const valarray& left, const valarray& right); template valarray operator*(const valarray left, const Ty& right); template valarray operator*(const Ty& left, const valarray& right);Copy to clipboard The first template operator returns an object of class `valarray`, each of whose elements `I` is `left[I] * right[I]`. The second template operator stores in element `I` `left[I] * right`. The third template operator stores in element `I` `left * right[I]`. ## `operator+` template valarray operator+(const valarray& left, const valarray& right); template valarray operator+(const valarray left, const Ty& right); template valarray operator+(const Ty& left, const valarray& right);Copy to clipboard The first template operator returns an object of class `valarray`, each of whose elements `I` is `left[I] + right[I]`. The second template operator stores in element `I` `left[I] + right`. The third template operator stores in element `I` `left + right[I]`. ## `operator-` template valarray operator-(const valarray& left, const valarray& right); template valarray operator-(const valarray left, const Ty& right); template valarray operator-(const Ty& left, const valarray& right);Copy to clipboard The first template operator returns an object of class `valarray`, each of whose elements `I` is `left[I] - right[I]`. The second template operator stores in element `I` `left[I] - right`. The third template operator stores in element `I` `left - right[I]`. ## `operator/` template valarray operator/(const valarray& left, const valarray& right); template valarray operator/(const valarray left, const Ty& right); template valarray operator/(const Ty& left, const valarray& right);Copy to clipboard The first template operator returns an object of class `valarray`, each of whose elements `I` is `left[I] / right[I]`. The second template operator stores in element `I` `left[I] / right`. The third template operator stores in element `I` `left / right[I]`. ## `operator==` template valarray operator==(const valarray& left, const valarray& right); template valarray operator==(const valarray left, const Ty& right); template valarray operator==(const Ty& left const valarray& right);Copy to clipboard The first template operator returns an object of class `valarray`, each of whose elements `I` is `left[I] == right[I]`. The second template operator stores in element `I` `left[I] == right`. The third template operator stores in element `I` `left == right[I]`. ## `operator^` template valarray operator^(const valarray& left, const valarray& right); template valarray operator^(const valarray left, const Ty& right); template valarray operator^(const Ty& left, const valarray& right);Copy to clipboard The first template operator returns an object of class `valarray`, each of whose elements `I` is `left[I] ^ right[I]`. The second template operator stores in element `I` `left[I] ^ right`. The third template operator stores in element `I` `left ^ right[I]`. ## `operator|` template valarray operator|(const valarray& left, const valarray& right); template valarray operator|(const valarray left, const Ty& right); template valarray operator|(const Ty& left, const valarray& right);Copy to clipboard The first template operator returns an object of class `valarray`, each of whose elements `I` is `left[I] | right[I]`. The second template operator stores in element `I` `left[I] | right`. The third template operator stores in element `I` `left | right[I]`. ## `operator||` template valarray operator||(const valarray& left, const valarray& right); template valarray operator||(const valarray left, const Ty& right); template valarray operator||(const Ty& left, const valarray& right);Copy to clipboard The first template operator returns an object of class `valarray`, each of whose elements `I` is `left[I] || right[I]`. The second template operator stores in element `I` `left[I] || right`. The third template operator stores in element `I` `left || right[I]`. ## `pow` template valarray pow(const valarray& left, const valarray& right); template valarray pow(const valarray left, const Ty& right); template valarray pow(const Ty& left, const valarray& right);Copy to clipboard The first template function returns an object of class `valarray`, each of whose elements `I` is `left[I]` raised to the `right[I]` power. The second template function stores in element `I` `left[I]` raised to the `right` power. The third template function stores in element `I` `left` raised to the `right[I]` power. ## `sin` template valarray sin(const valarray& left);Copy to clipboard The template function returns an object of class `valarray`, each of whose elements `I` is the sine of `left[I]`. ## `sinh` template valarray sinh(const valarray& left);Copy to clipboard The template function returns an object of class `valarray`, each of whose elements `I` is the hyperbolic sine of `left[I]`. ## `slice` class slice { public: slice(); slice(size_t st, size_t len, size_t str); size_t start() const; size_t size() const; size_t stride() const; };Copy to clipboard The class stores the parameters that characterize a `slice_array` when an object of class `slice` appears as a subscript for an object of class `valarray`. The stored values include: - a **starting index** - a **total length** - a **stride**, or distance between subsequent indices ### `slice::slice` slice(); slice(size_t st, const valarray len, const valarray str);Copy to clipboard The default constructor stores zeros for the starting index, total length, and stride. The second constructor stores `st` for the starting index, `len` for the total length, and `str` for the stride. ### `slice::size` size_t size() const;Copy to clipboard The member function returns the stored total length. ### `slice::start` size_t start() const;Copy to clipboard The member function returns the stored starting index. ### `slice::stride` size_t stride() const;Copy to clipboard The member function returns the stored stride. ## `slice_array` template class slice_array { public: typedef Ty value_type; void operator=(const valarray right) const; void operator*=(const valarray right) const; void operator/=(const valarray right) const; void operator%=(const valarray right) const; void operator+=(const valarray right) const; void operator-=(const valarray right) const; void operator^=(const valarray right) const; void operator&=(const valarray right) const; void operator|=(const valarray right) const; void operator<<=(const valarray right) const; void operator>>=(const valarray right) const; private: void slice_array(); // not defined void slice_array( const slice_array&); // not defined slice_array& operator=( const slice_array&); // not defined };Copy to clipboard The class describes an object that stores a reference to an object `va` of class `valarray`, along with an object `sl` of class `slice` which describes the sequence of elements to select from the `valarray` object. You construct a `slice_array` object only by writing an expression of the form `va[sl]`. The member functions of class `slice_array` then behave like the corresponding function signatures defined for `valarray`, except that only the sequence of selected elements is affected. The sequence consists of `sl.size()` elements, where element `I` becomes the index `sl.start() + I * sl.stride()` within `va`. For example: // va[slice(2, 5, 3)] selects elements with // indices 2, 5, 8, 11, 14Copy to clipboard ## `sqrt` template valarray sqrt(const valarray& left);Copy to clipboard The template function returns an object of class `valarray`, each of whose elements `I` is the square root of `left[I]`. ## `tan` template valarray tan(const valarray& left);Copy to clipboard The template function returns an object of class `valarray`, each of whose elements `I` is the tangent of `left[I]`. ## `tanh` template valarray tanh(const valarray& left);Copy to clipboard The template function returns an object of class `valarray`, each of whose elements `I` is the hyperbolic tangent of `left[I]`. ## `valarray` * * * **`apply· cshift· max· min· operator!· operator%=· operator&=· operator>>=· operator<<=· operator*=· operator+· operator+=· operator-· operator-=· operator/=· operator=· operator[]· operator^=· operator|=· operator~· resize· shift· size· sum· valarray· value_type`** * * * template class valarray { public: typedef Ty value_type; valarray(); explicit valarray(size_t count); valarray(const Ty& val, size_t count)); valarray(const Ty *ptr, size_t count); valarray(const slice_array& slicearr); valarray(const gslice_array& gslicearr); valarray(const mask_array& maskarr); valarray(const indirect_array& indarr); valarray& operator=(const valarray& right); valarray& operator=(const Ty& val); valarray& operator=(const slice_array& slicearr); valarray& operator=(const gslice_array& gslicearr); valarray& operator=(const mask_array& maskarr); valarray& operator=(const indirect_array& indarr); Ty& operator[](size_t off); slice_array operator[](slice slicearr); gslice_array operator[](const gslice& gslicearr); mask_array operator[](const valarray& boolarr); indirect_array operator[](const valarray& indarr); Ty operator[](size_t off) const; valarray operator[](slice slicearr) const; valarray operator[](const gslice& gslicearr) const; valarray operator[](const valarray& boolarr) const; valarray operator[](const valarray& indarr) const; valarray operator+() const; valarray operator-() const; valarray operator~() const; valarray operator!() const; valarray& operator*=(const valarray& right); valarray& operator*=(const Ty& right); valarray& operator/=(const valarray& right); valarray& operator/=(const Ty& right); valarray& operator%=(const valarray& right); valarray& operator%=(const Ty& right); valarray& operator+=(const valarray& right); valarray& operator+=(const Ty& right); valarray& operator-=(const valarray& right); valarray& operator-=(const Ty& right); valarray& operator^=(const valarray& right); valarray& operator^=(const Ty& right); valarray& operator&=(const valarray& right); valarray& operator&=(const Ty& right); valarray& operator|=(const valarray& right); valarray& operator|=(const Ty& right); valarray& operator<<=(const valarray& right); valarray& operator<<=(const Ty& right); valarray& operator>>=(const valarray& right); valarray& operator>>=(const Ty& right); size_t size() const; Ty sum() const; Ty max() const; Ty min() const; valarray shift(int count) const; valarray cshift(int count) const; valarray apply(Ty func(Ty)) const; valarray apply(Ty func(const Ty&)) const; void resize(size_t newsize); void resize(size_t newsize, const Ty& val); };Copy to clipboard The template class describes an object that controls a varying-length sequence of elements of type `Ty`. The sequence is stored as an array of `Ty`. It differs from template class `vector` in two important ways: - It defines numerous arithmetic operations between corresponding elements of `valarray` objects of the same type and length, such as `xarr = cos(yarr) + sin(zarr)`. - It defines a variety of interesting ways to subscript a `valarray` object, by overloading `operator[]`. An object of class `Ty`: - has a public default constructor, destructor, copy constructor, and assignment operator -- with conventional behavior - defines the arithmetic operators and math functions, as needed, that are defined for the floating-point types -- with conventional behavior In particular, no subtle differences may exist between copy construction and default construction followed by assignment. And none of the operations on objects of class `Ty` may throw exceptions. ### `valarray::apply` valarray apply(Ty func(Ty)) const; valarray apply(Ty func(const Ty&)) const;Copy to clipboard The member function returns an object of class `valarray`, of length `size()`, each of whose elements `I` is `func((*this)[I])`. ### `valarray::cshift` valarray cshift(int count) const;Copy to clipboard The member function returns an object of class `valarray`, of length `size()`, each of whose elements `I` is `(*this)[(I + count) % size()]`. Thus, if element zero is taken as the leftmost element, a positive value of `count` shifts the elements circularly left `count` places. ### `valarray::max` Ty max() const;Copy to clipboard The member function returns the value of the largest element of `*this`, which must have nonzero length. If the length is greater than one, it compares values by applying `operator<` between pairs of corresponding elements of class `Ty`. ### `valarray::min` Ty min() const;Copy to clipboard The member function returns the value of the smallest element of `*this`, which must have nonzero length. If the length is greater than one, it compares values by applying `operator<` between pairs of elements of class `Ty`. ### `valarray::operator!` valarray operator!() const;Copy to clipboard The member operator returns an object of class `valarray`, of length `size()`, each of whose elements `I` is `!(*this)`. ### `valarray::operator%=` valarray& operator%=(const valarray& right); valarray& operator%=(const Ty& right);Copy to clipboard The member operator replaces each element `I` of `*this` with `(*this)[I] % right[I]`. It returns `*this`. ### `valarray::operator&=` valarray& operator&=(const valarray& right); valarray& operator&=(const Ty& right);Copy to clipboard The member operator replaces each element `I` of `*this` with `(*this)[I] & right[I]`. It returns `*this`. ### `valarray::operator>>=` valarray& operator>>=(const valarray& right); valarray& operator>>=(const Ty& right);Copy to clipboard The member operator replaces each element `I` of `*this` with `(*this)[I] >> right[I]`. It returns `*this`. ### `valarray::operator<<=` valarray& operator<<=(const valarray& right); valarray& operator<<=(const Ty& right);Copy to clipboard The member operator replaces each element `I` of `*this` with `(*this)[I] << right[I]`. It returns `*this`. ### `valarray::operator*=` valarray& operator*=(const valarray& right); valarray& operator*=(const Ty& right);Copy to clipboard The member operator replaces each element `I` of `*this` with `(*this)[I] * right[I]`. It returns `*this`. ### `valarray::operator+` valarray operator+() const;Copy to clipboard The member operator returns an object of class `valarray`, of length `size()`, each of whose elements `I` is `(*this)[I]`. ### `valarray::operator+=` valarray& operator+=(const valarray& right); valarray& operator+=(const Ty& right);Copy to clipboard The member operator replaces each element `I` of `*this` with `(*this)[I] + right[I]`. It returns `*this`. ### `valarray::operator-` valarray operator-() const;Copy to clipboard The member operator returns an object of class `valarray`, of length `size()`, each of whose elements `I` is `-(*this)[I]`. ### `valarray::operator-=` valarray& operator-=(const valarray& right); valarray& operator-=(const Ty& right);Copy to clipboard The member operator replaces each element `I` of `*this` with `(*this)[I] - right[I]`. It returns `*this`. ### `valarray::operator/=` valarray& operator/=(const valarray& right); valarray& operator/=(const Ty& right);Copy to clipboard The member operator replaces each element `I` of `*this` with `(*this)[I] / right[I]`. It returns `*this`. ### `valarray::operator=` valarray& operator=(const valarray& right); valarray& operator=(const Ty& val); valarray& operator=(const slice_array& slicearr); valarray& operator=(const gslice_array& gslicearr); valarray& operator=(const mask_array& maskarr); valarray& operator=(const indirect_array& indarr);Copy to clipboard The first member operator replaces the controlled sequence with a copy of the sequence controlled by `right`. The second member operator replaces each element of the controlled sequence with a copy of `val`. The remaining member operators replace those elements of the controlled sequence selected by their arguments, which are generated only by `operator[]`. If the value of a member in the replacement controlled sequence depends on a member in the initial controlled sequence, the result is undefined. If the length of the controlled sequence changes, the result is generally undefined. In this [implementation](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_index.html#implementation), however, the effect is merely to invalidate any pointers or references to elements in the controlled sequence. ### `valarray::operator[]` Ty& operator[](size_t off); slice_array operator[](slice slicearr); gslice_array operator[](const gslice& gslicearr); mask_array operator[](const valarray& boolarr); indirect_array operator[](const valarray& indarr); Ty operator[](size_t off) const; valarray operator[](slice slicearr) const; valarray operator[](const gslice& gslicearr) const; valarray operator[](const valarray& boolarr) const; valarray operator[](const valarray& indarr) const;Copy to clipboard The member operator is overloaded to provide several ways to select sequences of elements from among those controlled by `*this`. The first group of five member operators work in conjunction with various overloads of `operator=` (and other assigning operators) to allow selective replacement (slicing) of the controlled sequence. The selected elements must exist. The first member operator selects element `off`. For example: valarray v0("abcdefghijklmnop", 16); v0[3] = 'A'; // v0 == valarray("abcAefghijklmnop", 16)Copy to clipboard The second member operator selects those elements of the controlled sequence designated by `slicearr`. For example: valarray v0("abcdefghijklmnop", 16); valarray v1("ABCDE", 5); v0[slice(2, 5, 3)] = v1; // v0 == valarray("abAdeBghCjkDmnEp", 16)Copy to clipboard The third member operator selects those elements of the controlled sequence designated by `gslicearr`. For example: valarray v0("abcdefghijklmnop", 16); valarray v1("ABCDEF", 6); const size_t lv[] = {2, 3}; const size_t dv[] = {7, 2}; const valarray len(lv, 2), str(dv, 2); v0[gslice(3, len, str)] = v1; // v0 == valarray("abcAeBgCijDlEnFp", 16)Copy to clipboard The fourth member operator selects those elements of the controlled sequence designated by `boolarr`. For example: valarray v0("abcdefghijklmnop", 16); valarray v1("ABC", 3); const bool vb[] = {false, false, true, true, false, true}; v0[valarray(vb, 6)] = v1; // v0 == valarray("abABeCghijklmnop", 16)Copy to clipboard The fifth member operator selects those elements of the controlled sequence designated by `indarr`. For example: valarray v0("abcdefghijklmnop", 16); valarray v1("ABCDE", 5); const size_t vi[] = {7, 5, 2, 3, 8}; v0[valarray(vi, 5)] = v1; // v0 == valarray("abCDeBgAEjklmnop", 16)Copy to clipboard The second group of five member operators each construct an object that represents the value(s) selected. The selected elements must exist. The sixth member operator returns the value of element `off`. For example: valarray v0("abcdefghijklmnop", 16); // v0[3] returns 'd'Copy to clipboard The seventh member operator returns an object of class `valarray` containing those elements of the controlled sequence designated by `slicearr`. For example: valarray v0("abcdefghijklmnop", 16); // v0[slice(2, 5, 3)] returns valarray("cfilo", 5)Copy to clipboard The eighth member operator selects those elements of the controlled sequence designated by `gslicearr`. For example: valarray v0("abcdefghijklmnop", 16); const size_t lv[] = {2, 3}; const size_t dv[] = {7, 2}; const valarray len(lv, 2), str(dv, 2); // v0[gslice(3, len, str)] returns // valarray("dfhkmo", 6)Copy to clipboard The ninth member operator selects those elements of the controlled sequence designated by `boolarr`. For example: valarray v0("abcdefghijklmnop", 16); const bool vb[] = {false, false, true, true, false, true}; // v0[valarray(vb, 6)] returns // valarray("cdf", 3)Copy to clipboard The last member operator selects those elements of the controlled sequence designated by `indarr`. For example: valarray v0("abcdefghijklmnop", 16); const size_t vi[] = {7, 5, 2, 3, 8}; // v0[valarray(vi, 5)] returns // valarray("hfcdi", 5)Copy to clipboard ### `valarray::operator^=` valarray& operator^=(const valarray& right); valarray& operator^=(const Ty& right);Copy to clipboard The member operator replaces each element `I` of `*this` with `(*this)[I] ^ right[I]`. It returns `*this`. ### `valarray::operator|=` valarray& operator|=(const valarray& right); valarray& operator|=(const Ty& right);Copy to clipboard The member operator replaces each element `I` of `*this` with `(*this)[I] | right[I]`. It returns `*this`. ### `valarray::operator~` valarray operator~() const;Copy to clipboard The member operator returns an object of class `valarray`, of length `size()`, each of whose elements `I` is `~(*this)[I]`. ### `valarray::resize` void resize(size_t newsize); void resize(size_t newsize, const Ty& val);Copy to clipboard The member functions both ensure that `size()` henceforth returns `newsize`. The first member function initializes all elements to `Ty()`, while the second member function initializes all elements to `val`. Any pointers or references to elements in the controlled sequence are invalidated. ### `valarray::shift` valarray shift(int count) const;Copy to clipboard The member function returns an object of class `valarray`, of length `size()`, each of whose elements `I` is either `(*this)[I + count]`, if `I + count` is a valid subscript, or `Ty()`. Thus, if element zero is taken as the leftmost element, a positive value of `count` shifts the elements left `count` places, with zero fill. ### `valarray::size` size_t size() const;Copy to clipboard The member function returns the number of elements in the array. ### `valarray::sum` Ty sum() const;Copy to clipboard The member function returns the sum of all elements of `*this`, which must have nonzero length. If the length is greater than one, it adds values to the sum by applying `operator+=` between pairs of elements of class `Ty`. ### `valarray::valarray` valarray(); explicit valarray(size_t count); valarray(const Ty& val, size_t count)); valarray(const Ty *ptr, size_t count); valarray(const slice_array& slicearr); valarray(const gslice_array& gslicearr); valarray(const mask_array& maskarr); valarray(const indirect_array& indarr);Copy to clipboard The first (default) constructor initializes the object to an empty array. The next three constructors each initialize the object to an array of `count` elements as follows: - For `explicit valarray(size_t count)`, each element is initialized with the default constructor. - For `valarray(const Ty& val, size_t count))`, each element is initialized with `val`. - For `valarray(const Ty *ptr, size_t count)`, the element at position `I` is initialized with `p[I]`. Each of the remaining constructors initializes the object to a `valarray` object determined by the argument. ### `valarray::value_type` typedef Ty value_type;Copy to clipboard The type is a synonym for the template parameter `Ty`. ## `valarray` class valarrayCopy to clipboard The type is a specialization of template class `valarray`, for elements of type `bool`. * * * See also the **[Table of Contents](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_index.html#Table%20of%20Contents)** and the **[Index](https://docs.qualcomm.com/doc/80-N3040-23/topic/_cpp_index.html)**. *[Copyright](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_crit_pjp.html) © 1992-2006by P.J. Plauger. All rights reserved.* Last Published: Oct 06, 2026 Previous Topic Next Topic