Working Draft, Standard for Programming Language C++ (N4713, 2017 year) - page 34

 

  Главная      Manuals     Working Draft, Standard for Programming Language C++ (N4713, 2017 year)

 

Search            copyright infringement  

 

 

 

 

 

 

 

 

 

 

 

Content      ..     32      33      34      35     ..

 

 

 

Working Draft, Standard for Programming Language C++ (N4713, 2017 year) - page 34

 

 

// property functions
RealType m() const;
RealType n() const;
param_type param() const;
void param(const param_type& parm);
result_type min() const;
result_type max() const;
};
explicit fisher_f_distribution(RealType m = 1, RealType n = 1);
2
Requires: 0 < m and 0 < n.
3
Effects: Constructs a fisher_f_distribution object; m and n correspond to the respective parameters
of the distribution.
RealType m() const;
4
Returns: The value of the m parameter with which the object was constructed.
RealType n() const;
5
Returns: The value of the n parameter with which the object was constructed.
29.6.8.5.6
Class template student_t_distribution
[rand.dist.norm.t]
1
A student_t_distribution random number distribution produces random numbers x distributed according
to the probability density function
(
)
(
1
Γ
(n + 1)/2
x2 )−(n+1)/2
p(x | n) =
·
·
1+
√nπ
Γ(n/2)
n
template<class RealType = double>
class student_t_distribution {
public:
// types
using result_type = RealType;
using param_type
= unspecified ;
// constructor and reset functions
explicit student_t_distribution(RealType n = 1);
explicit student_t_distribution(const param_type& parm);
void reset();
// generating functions
template<class URBG>
result_type operator()(URBG& g);
template<class URBG>
result_type operator()(URBG& g, const param_type& parm);
// property functions
RealType n() const;
param_type param() const;
void param(const param_type& parm);
result_type min() const;
result_type max() const;
};
explicit student_t_distribution(RealType n = 1);
2
Requires: 0 < n.
3
Effects: Constructs a student_t_distribution object; n corresponds to the parameter of the distri-
bution.
RealType n() const;
4
Returns: The value of the n parameter with which the object was constructed.
§ 29.6.8.5.6
982
29.6.8.6
Sampling distributions
[rand.dist.samp]
29.6.8.6.1
Class template discrete_distribution
[rand.dist.samp.discrete]
1
A discrete_distribution random number distribution produces random integers i, 0 ≤ i < n, distributed
according to the discrete probability function
P (i | p0, . . . , pn−1) = pi .
2
Unless specified otherwise, the distribution parameters are calculated as: pk = wk/S for k = 0,...,n−1 ,
in which the values wk , commonly known as the weights, shall be non-negative, non-NaN, and non-infinity.
Moreover, the following relation shall hold: 0 < S = w0 + · · · + wn−1.
template<class IntType = int>
class discrete_distribution {
public:
// types
using result_type = IntType;
using param_type
= unspecified ;
// constructor and reset functions
discrete_distribution();
template<class InputIterator>
discrete_distribution(InputIterator firstW, InputIterator lastW);
discrete_distribution(initializer_list<double> wl);
template<class UnaryOperation>
discrete_distribution(size_t nw, double xmin, double xmax, UnaryOperation
fw);
explicit discrete_distribution(const param_type& parm);
void reset();
// generating functions
template<class URBG>
result_type operator()(URBG& g);
template<class URBG>
result_type operator()(URBG& g, const param_type& parm);
// property functions
vector<double> probabilities() const;
param_type param() const;
void param(const param_type& parm);
result_type min() const;
result_type max() const;
};
discrete_distribution();
3
Effects: Constructs a discrete_distribution object with n = 1 and p0 = 1. [ Note: Such an object
will always deliver the value 0.
— end note ]
template<class InputIterator>
discrete_distribution(InputIterator firstW, InputIterator lastW);
4
Requires: InputIterator shall satisfy the requirements of an input iterator (27.2.3). Moreover,
iterator_traits<InputIterator>::value_type shall denote a type that is convertible to double.
[
)
If firstW == lastW, let n = 1 and w0 = 1. Otherwise,
firstW, lastW
shall form a sequence w of
length n > 0.
5
Effects: Constructs a discrete_distribution object with probabilities given by the formula above.
discrete_distribution(initializer_list<double> wl);
6
Effects: Same as discrete_distribution(wl.begin(), wl.end()).
template<class UnaryOperation>
discrete_distribution(size_t nw, double xmin, double xmax, UnaryOperation fw);
7
Requires: Each instance of type UnaryOperation shall be a function object (23.14) whose return type
shall be convertible to double. Moreover, double shall be convertible to the type of UnaryOperation’s
§ 29.6.8.6.1
983
sole parameter. If nw = 0, let n = 1, otherwise let n = nw. The relation 0 < δ = (xmax − xmin)/n shall
hold.
8
Effects: Constructs a discrete_distribution object with probabilities given by the formula above,
using the following values: If nw = 0, let w0 = 1. Otherwise, let wk = fw(xmin + k · δ + δ/2) for
k = 0,...,n−1.
9
Complexity: The number of invocations of fw shall not exceed n.
vector<double> probabilities() const;
10
Returns: A vector<double> whose size member returns n and whose operator[] member returns
pk when invoked with argument k for k = 0,...,n−1.
29.6.8.6.2
Class template piecewise_constant_distribution
[rand.dist.samp.pconst]
1
A piecewise_constant_distribution random number distribution produces random numbers x, b0 ≤ x <
bn, uniformly distributed over each subinterval [bi,bi+1) according to the probability density function
p(x | b0, . . . , bn, ρ0, . . . , ρn−1) = ρi , for bi ≤ x < bi+1 .
2
The n + 1 distribution parameters bi, also known as this distribution’s interval boundaries, shall satisfy the
relation bi < bi+1 for i = 0, . . . , n−1. Unless specified otherwise, the remaining n distribution parameters are
calculated as:
wk
ρk =
for k = 0, . . . , n−1,
S · (bk+1 − bk)
in which the values wk , commonly known as the weights, shall be non-negative, non-NaN, and non-infinity.
Moreover, the following relation shall hold: 0 < S = w0 + · · · + wn−1.
template<class RealType = double>
class piecewise_constant_distribution {
public:
// types
using result_type = RealType;
using param_type
= unspecified ;
// constructor and reset functions
piecewise_constant_distribution();
template<class InputIteratorB, class InputIteratorW>
piecewise_constant_distribution(InputIteratorB firstB, InputIteratorB lastB,
InputIteratorW firstW);
template<class UnaryOperation>
piecewise_constant_distribution(initializer_list<RealType> bl, UnaryOperation
fw);
template<class UnaryOperation>
piecewise_constant_distribution(size_t nw, RealType xmin, RealType xmax,
UnaryOperation fw);
explicit piecewise_constant_distribution(const param_type& parm);
void reset();
// generating functions
template<class URBG>
result_type operator()(URBG& g);
template<class URBG>
result_type operator()(URBG& g, const param_type& parm);
// property functions
vector<result_type> intervals() const;
vector<result_type> densities() const;
param_type param() const;
void param(const param_type& parm);
result_type min() const;
result_type max() const;
};
§ 29.6.8.6.2
984
piecewise_constant_distribution();
3
Effects: Constructs a piecewise_constant_distribution object with n = 1, ρ0 = 1, b0 = 0, and
b1 = 1.
template<class InputIteratorB, class InputIteratorW>
piecewise_constant_distribution(InputIteratorB firstB, InputIteratorB lastB,
InputIteratorW firstW);
4
Requires: InputIteratorB and InputIteratorW shall each satisfy the requirements of an input iter-
ator (Table 95) type. Moreover, iterator_traits<InputIteratorB>::value_type and iterator_-
traits<InputIteratorW>::value_type shall each denote a type that is convertible to double. If
firstB == lastB or ++firstB == lastB, let n = 1, w0 = 1, b0 = 0, and b1 = 1. Otherwise,
[
)
firstB, lastB
shall form a sequence b of length n + 1, the length of the sequence w starting from
firstW shall be at least n, and any wk for k ≥ n shall be ignored by the distribution.
5
Effects: Constructs a piecewise_constant_distribution object with parameters as specified above.
template<class UnaryOperation>
piecewise_constant_distribution(initializer_list<RealType> bl, UnaryOperation fw);
6
Requires: Each instance of type UnaryOperation shall be a function object (23.14) whose return type
shall be convertible to double. Moreover, double shall be convertible to the type of UnaryOperation’s
sole parameter.
7
Effects: Constructs a piecewise_constant_distribution object with parameters taken or calculated
from the following values: If bl.size() < 2, let n = 1, w0 = 1, b0 = 0, and b1 = 1. Otherwise, let
[
)
((
)
)
bl.begin(), bl.end()
form a sequence b0, . . . , bn, and let wk = fw
bk+1 +bk
/2
for k = 0, . . . , n−1.
8
Complexity: The number of invocations of fw shall not exceed n.
template<class UnaryOperation>
piecewise_constant_distribution(size_t nw, RealType xmin, RealType xmax, UnaryOperation fw);
9
Requires: Each instance of type UnaryOperation shall be a function object (23.14) whose return type
shall be convertible to double. Moreover, double shall be convertible to the type of UnaryOperation’s
sole parameter. If nw = 0, let n = 1, otherwise let n = nw. The relation 0 < δ = (xmax − xmin)/n shall
hold.
10
Effects: Constructs a piecewise_constant_distribution object with parameters taken or calculated
from the following values: Let bk = xmin+k ·δ for k = 0, . . . , n, and wk = fw(bk +δ/2) for k = 0, . . . , n−1.
11
Complexity: The number of invocations of fw shall not exceed n.
vector<result_type> intervals() const;
12
Returns: A vector<result_type> whose size member returns n + 1 and whose operator[] member
returns bk when invoked with argument k for k = 0, . . . , n.
vector<result_type> densities() const;
13
Returns: A vector<result_type> whose size member returns n and whose operator[] member
returns ρk when invoked with argument k for k = 0, . . . , n−1.
29.6.8.6.3
Class template piecewise_linear_distribution
[rand.dist.samp.plinear]
1
A piecewise_linear_distribution random number distribution produces random numbers x, b0 ≤ x < bn,
distributed over each subinterval [bi, bi+1) according to the probability density function
bi+1 − x
x−bi
p(x | b0, . . . , bn, ρ0, . . . , ρn) = ρi ·
i+1 ·
, for bi ≤ x < bi+1 .
bi+1 − bi
bi+1 − bi
2
The n + 1 distribution parameters bi, also known as this distribution’s interval boundaries, shall satisfy the
relation bi < bi+1 for i = 0, . . . , n−1. Unless specified otherwise, the remaining n + 1 distribution parameters
are calculated as ρk = wk/S for k = 0,...,n, in which the values wk, commonly known as the weights at
boundaries, shall be non-negative, non-NaN, and non-infinity. Moreover, the following relation shall hold:
1
0<S =
(wk + wk+1) · (bk+1 − bk ) .
2 ·
k=0
§ 29.6.8.6.3
985
template<class RealType = double>
class piecewise_linear_distribution {
public:
// types
using result_type = RealType;
using param_type
= unspecified ;
// constructor and reset functions
piecewise_linear_distribution();
template<class InputIteratorB, class InputIteratorW>
piecewise_linear_distribution(InputIteratorB firstB, InputIteratorB lastB,
InputIteratorW firstW);
template<class UnaryOperation>
piecewise_linear_distribution(initializer_list<RealType> bl, UnaryOperation fw);
template<class UnaryOperation>
piecewise_linear_distribution(size_t nw, RealType xmin, RealType xmax, UnaryOperation
fw);
explicit piecewise_linear_distribution(const param_type& parm);
void reset();
// generating functions
template<class URBG>
result_type operator()(URBG& g);
template<class URBG>
result_type operator()(URBG& g, const param_type& parm);
// property functions
vector<result_type> intervals() const;
vector<result_type> densities() const;
param_type param() const;
void param(const param_type& parm);
result_type min() const;
result_type max() const;
};
piecewise_linear_distribution();
3
Effects: Constructs a piecewise_linear_distribution object with n = 1, ρ0 = ρ1 = 1, b0 = 0, and
b1 = 1.
template<class InputIteratorB, class InputIteratorW>
piecewise_linear_distribution(InputIteratorB firstB, InputIteratorB lastB,
InputIteratorW firstW);
4
Requires: InputIteratorB and InputIteratorW shall each satisfy the requirements of an input iter-
ator (Table 95) type. Moreover, iterator_traits<InputIteratorB>::value_type and iterator_-
traits<InputIteratorW>::value_type shall each denote a type that is convertible to double. If
firstB == lastB or ++firstB == lastB, let n = 1, ρ0 = ρ1 = 1, b0 = 0, and b1 = 1. Otherwise,
[
)
firstB, lastB
shall form a sequence b of length n + 1, the length of the sequence w starting from
firstW shall be at least n + 1, and any wk for k ≥ n + 1 shall be ignored by the distribution.
5
Effects: Constructs a piecewise_linear_distribution object with parameters as specified above.
template<class UnaryOperation>
piecewise_linear_distribution(initializer_list<RealType> bl, UnaryOperation fw);
6
Requires: Each instance of type UnaryOperation shall be a function object (23.14) whose return type
shall be convertible to double. Moreover, double shall be convertible to the type of UnaryOperation’s
sole parameter.
7
Effects: Constructs a piecewise_linear_distribution object with parameters taken or calculated
from the following values: If bl.size() < 2, let n = 1, ρ0 = ρ1 = 1, b0 = 0, and b1 = 1. Otherwise, let
[
)
bl.begin(),bl.end()
form a sequence b0, . . . , bn, and let wk = fw(bk ) for k = 0, . . . , n.
8
Complexity: The number of invocations of fw shall not exceed n + 1.
§ 29.6.8.6.3
986
template<class UnaryOperation>
piecewise_linear_distribution(size_t nw, RealType xmin, RealType xmax, UnaryOperation fw);
9
Requires: Each instance of type UnaryOperation shall be a function object (23.14) whose return type
shall be convertible to double. Moreover, double shall be convertible to the type of UnaryOperation’s
sole parameter. If nw = 0, let n = 1, otherwise let n = nw. The relation 0 < δ = (xmax − xmin)/n shall
hold.
10
Effects: Constructs a piecewise_linear_distribution object with parameters taken or calculated
from the following values: Let bk = xmin + k · δ for k = 0, . . . , n, and wk = fw(bk ) for k = 0, . . . , n.
11
Complexity: The number of invocations of fw shall not exceed n + 1.
vector<result_type> intervals() const;
12
Returns: A vector<result_type> whose size member returns n + 1 and whose operator[] member
returns bk when invoked with argument k for k = 0, . . . , n.
vector<result_type> densities() const;
13
Returns: A vector<result_type> whose size member returns n and whose operator[] member
returns ρk when invoked with argument k for k = 0, . . . , n.
29.6.9
Low-quality random number generation
[c.math.rand]
1
[ Note: The header <cstdlib> (21.2.2) declares the functions described in this subclause.
— end note ]
int rand();
void srand(unsigned int seed);
2
Effects: The rand and srand functions have the semantics specified in the C standard library.
3
Remarks: The implementation may specify that particular library functions may call rand. It is
implementation-defined whether the rand function may introduce data races (20.5.5.9). [Note: The
other random number generation facilities in this document (29.6) are often preferable to rand, because
rand’s underlying algorithm is unspecified. Use of rand therefore continues to be non-portable, with
unpredictable and oft-questionable quality and performance.
— end note ]
See also: ISO C 7.22.2
29.7
Numeric arrays
[numarray]
29.7.1
Header <valarray> synopsis
[valarray.syn]
#include <initializer_list>
namespace std {
template<class T> class valarray;
// An array of type T
class slice;
// a BLAS-like slice out of an array
template<class T> class slice_array;
class gslice;
// a generalized slice out of an array
template<class T> class gslice_array;
template<class T> class mask_array;
// a masked array
template<class T> class indirect_array;
// an indirected array
template<class T> void swap(valarray<T>&, valarray<T>&) noexcept;
template<class T> valarray<T> 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 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 T&);
template<class T> valarray<T> operator/ (const 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 T&);
template<class T> valarray<T> operator% (const T&, const valarray<T>&);
§ 29.7.1
987
template<class
T>
valarray<T>
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
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 T&);
template<class
T>
valarray<T>
operator- (const
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 T&);
template<class
T>
valarray<T>
operator^ (const
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 T&);
template<class
T>
valarray<T>
operator& (const
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 T&);
template<class
T>
valarray<T>
operator| (const
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 T&);
template<class
T>
valarray<T>
operator<<(const
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 T&);
template<class
T>
valarray<T>
operator>>(const
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 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 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 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 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 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 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 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 T&, const valarray<T>&);
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> 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>&);
§
29.7.1
988
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> 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>&);
template<class T> valarray<T> sin
(const valarray<T>&);
template<class T> valarray<T> sinh (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> unspecified 1 begin(valarray<T>& v);
template<class T> unspecified 2 begin(const valarray<T>& v);
template<class T> unspecified 1 end(valarray<T>& v);
template<class T> unspecified 2 end(const valarray<T>& v);
}
1
The header <valarray> defines five class templates (valarray, slice_array, gslice_array, mask_array,
and indirect_array), two classes (slice and gslice), and a series of related function templates for
representing and manipulating arrays of values.
2
The valarray array classes are defined to be free of certain forms of aliasing, thus allowing operations on
these classes to be optimized.
3
Any function returning a valarray<T> is permitted to return an object of another type, provided all the
const member functions of valarray<T> are also applicable to this type. This return type shall not add
more than two levels of template nesting over the most deeply nested argument type.278
4
Implementations introducing such replacement types shall provide additional functions and operators as
follows:
(4.1)
for every function taking a const valarray<T>& other than begin and end (29.7.10), identical functions
taking the replacement types shall be added;
(4.2)
for every function taking two const valarray<T>& arguments, identical functions taking every combi-
nation of const valarray<T>& and replacement types shall be added.
5
In particular, an implementation shall allow a valarray<T> to be constructed from such replacement types
and shall allow assignments and compound assignments of such types to valarray<T>, slice_array<T>,
gslice_array<T>, mask_array<T> and indirect_array<T> objects.
6
These library functions are permitted to throw a bad_alloc (21.6.3.1) exception if there are not sufficient
resources available to carry out the operation. Note that the exception is not mandated.
29.7.2
Class template valarray
[template.valarray]
29.7.2.1
Class template valarray overview
[template.valarray.overview]
namespace std {
template<class T> class valarray {
public:
using value_type = T;
// 29.7.2.2, construct/destroy
valarray();
explicit valarray(size_t);
valarray(const T&, size_t);
valarray(const T*, size_t);
valarray(const valarray&);
valarray(valarray&&) noexcept;
valarray(const slice_array<T>&);
278) Annex B recommends a minimum number of recursively nested template instantiations. This requirement thus indirectly
suggests a minimum allowable complexity for valarray expressions.
§ 29.7.2.1
989
valarray(const gslice_array<T>&);
valarray(const mask_array<T>&);
valarray(const indirect_array<T>&);
valarray(initializer_list<T>);
~valarray();
// 29.7.2.3, assignment
valarray& operator=(const valarray&);
valarray& operator=(valarray&&) noexcept;
valarray& operator=(initializer_list<T>);
valarray& operator=(const T&);
valarray& operator=(const slice_array<T>&);
valarray& operator=(const gslice_array<T>&);
valarray& operator=(const mask_array<T>&);
valarray& operator=(const indirect_array<T>&);
// 29.7.2.4, element access
const T&
operator[](size_t) const;
T&
operator[](size_t);
// 29.7.2.5, subset operations
valarray
operator[](slice) const;
slice_array<T>
operator[](slice);
valarray
operator[](const gslice&) const;
gslice_array<T>
operator[](const gslice&);
valarray
operator[](const valarray<bool>&) const;
mask_array<T>
operator[](const valarray<bool>&);
valarray
operator[](const valarray<size_t>&) const;
indirect_array<T> operator[](const valarray<size_t>&);
// 29.7.2.6, unary operators
valarray operator+() const;
valarray operator-() const;
valarray operator~() const;
valarray<bool> operator!() const;
// 29.7.2.7, compound assignment
valarray& operator*= (const T&);
valarray& operator/= (const T&);
valarray& operator%= (const T&);
valarray& operator+= (const T&);
valarray& operator-= (const T&);
valarray& operator^= (const T&);
valarray& operator&= (const T&);
valarray& operator|= (const T&);
valarray& operator<<=(const T&);
valarray& operator>>=(const T&);
valarray& operator*= (const valarray&);
valarray& operator/= (const valarray&);
valarray& operator%= (const valarray&);
valarray& operator+= (const valarray&);
valarray& operator-= (const valarray&);
valarray& operator^= (const valarray&);
valarray& operator|= (const valarray&);
valarray& operator&= (const valarray&);
valarray& operator<<=(const valarray&);
valarray& operator>>=(const valarray&);
// 29.7.2.8, member functions
void swap(valarray&) noexcept;
size_t size() const;
§
29.7.2.1
990
T sum() const;
T min() const;
T max() const;
valarray shift (int) const;
valarray cshift(int) const;
valarray apply(T func(T)) const;
valarray apply(T func(const T&)) const;
void resize(size_t sz, T c = T());
};
template<class T, size_t cnt> valarray(const T(&)[cnt], size_t) -> valarray<T>;
}
1
The class template valarray<T> is a one-dimensional smart array, with elements numbered sequentially from
zero. It is a representation of the mathematical concept of an ordered set of values. For convenience, an
object of type valarray<T> is referred to as an “array” throughout the remainder of 29.7. The illusion of
higher dimensionality may be produced by the familiar idiom of computed indices, together with the powerful
subsetting capabilities provided by the generalized subscript operators.279
29.7.2.2
valarray constructors
[valarray.cons]
valarray();
1
Effects: Constructs a valarray that has zero length.280
explicit valarray(size_t n);
2
Effects: Constructs a valarray that has length n. Each element of the array is value-initialized (11.6).
valarray(const T& v, size_t n);
3
Effects: Constructs a valarray that has length n. Each element of the array is initialized with v.
valarray(const T* p, size_t n);
4
Requires: p points to an array (11.3.4) of at least n elements.
Effects: Constructs a valarray that has length n. The values of the elements of the array are initialized
with the first n values pointed to by the first argument.281
valarray(const valarray& v);
5
Effects: Constructs a valarray that has the same length as v. The elements are initialized with the
values of the corresponding elements of v.282
valarray(valarray&& v) noexcept;
6
Effects: Constructs a valarray that has the same length as v. The elements are initialized with the
values of the corresponding elements of v.
7
Complexity: Constant.
valarray(initializer_list<T> il);
8
Effects: Equivalent to valarray(il.begin(), il.size()).
valarray(const slice_array<T>&);
valarray(const gslice_array<T>&);
valarray(const mask_array<T>&);
valarray(const indirect_array<T>&);
9
These conversion constructors convert one of the four reference templates to a valarray.
279) The intent is to specify an array template that has the minimum functionality necessary to address aliasing ambiguities
and the proliferation of temporary objects. Thus, the valarray template is neither a matrix class nor a field class. However, it
is a very useful building block for designing such classes.
280) This default constructor is essential, since arrays of valarray may be useful. After initialization, the length of an empty
array can be increased with the resize member function.
281) This constructor is the preferred method for converting a C array to a valarray object.
282) This copy constructor creates a distinct array rather than an alias. Implementations in which arrays share storage are
permitted, but they shall implement a copy-on-reference mechanism to ensure that arrays are conceptually distinct.
§ 29.7.2.2
991
~valarray();
10
Effects: The destructor is applied to every element of *this; an implementation may return all allocated
memory.
29.7.2.3
valarray assignment
[valarray.assign]
valarray& operator=(const valarray& v);
1
Effects: Each element of the *this array is assigned the value of the corresponding element of v. If the
length of v is not equal to the length of *this, resizes *this to make the two arrays the same length,
as if by calling resize(v.size()), before performing the assignment.
2
Postconditions: size() == v.size().
3
Returns: *this.
valarray& operator=(valarray&& v) noexcept;
4
Effects: *this obtains the value of v. The value of v after the assignment is not specified.
5
Returns: *this.
6
Complexity: Linear.
valarray& operator=(initializer_list<T> il);
7
Effects: Equivalent to: return *this = valarray(il);
valarray& operator=(const T& v);
8
Effects: Assigns v to each element of *this.
9
Returns: *this.
valarray& operator=(const slice_array<T>&);
valarray& operator=(const gslice_array<T>&);
valarray& operator=(const mask_array<T>&);
valarray& operator=(const indirect_array<T>&);
10
Requires: The length of the array to which the argument refers equals size(). The value of an element
in the left-hand side of a valarray assignment operator does not depend on the value of another
element in that left-hand side.
11
These operators allow the results of a generalized subscripting operation to be assigned directly to a
valarray.
29.7.2.4
valarray element access
[valarray.access]
const T& operator[](size_t n) const;
T& operator[](size_t n);
1
Requires: n < size().
2
Returns: A reference to the corresponding element of the array. [ Note: The expression (a[i] = q,
a[i]) == q evaluates to true for any non-constant valarray<T> a, any T q, and for any size_t i
such that the value of i is less than the length of a.
— end note ]
3
Remarks: The expression &a[i+j] == &a[i] + j evaluates to true for all size_t i and size_t j
such that i+j < a.size().
4
The expression &a[i] != &b[j] evaluates to true for any two arrays a and b and for any size_t i
and size_t j such that i < a.size() and j < b.size(). [ Note: This property indicates an absence
of aliasing and may be used to advantage by optimizing compilers. Compilers may take advantage
of inlining, constant propagation, loop fusion, tracking of pointers obtained from operator new, and
other techniques to generate efficient valarrays.
— end note ]
5
The reference returned by the subscript operator for an array shall be valid until the member function
resize(size_t, T) (29.7.2.8) is called for that array or until the lifetime of that array ends, whichever
happens first.
§ 29.7.2.4
992
29.7.2.5
valarray subset operations
[valarray.sub]
1
The member operator[] is overloaded to provide several ways to select sequences of elements from among
those controlled by *this. Each of these operations returns a subset of the array. The const-qualified versions
return this subset as a new valarray object. The non-const versions return a class template object which
has reference semantics to the original array, working in conjunction with various overloads of operator=
and other assigning operators to allow selective replacement (slicing) of the controlled sequence. In each case
the selected element(s) shall exist.
valarray operator[](slice slicearr) const;
2
Returns: A valarray containing those elements of the controlled sequence designated by slicearr.
[ Example:
const valarray<char> v0("abcdefghijklmnop", 16);
// v0[slice(2, 5, 3)] returns valarray<char>("cfilo", 5)
— end example ]
slice_array<T> operator[](slice slicearr);
3
Returns: An object that holds references to elements of the controlled sequence selected by slicearr.
[ Example:
valarray<char> v0("abcdefghijklmnop", 16);
valarray<char> v1("ABCDE", 5);
v0[slice(2, 5, 3)] = v1;
// v0 == valarray<char>("abAdeBghCjkDmnEp", 16);
— end example ]
valarray operator[](const gslice& gslicearr) const;
4
Returns: A valarray containing those elements of the controlled sequence designated by gslicearr.
[ Example:
const valarray<char> v0("abcdefghijklmnop", 16);
const size_t lv[] = { 2, 3 };
const size_t dv[] = { 7, 2 };
const valarray<size_t> len(lv, 2), str(dv, 2);
// v0[gslice(3, len, str)] returns
// valarray<char>("dfhkmo", 6)
— end example ]
gslice_array<T> operator[](const gslice& gslicearr);
5
Returns: An object that holds references to elements of the controlled sequence selected by gslicearr.
[ Example:
valarray<char> v0("abcdefghijklmnop", 16);
valarray<char> v1("ABCDEF", 6);
const size_t lv[] = { 2, 3 };
const size_t dv[] = { 7, 2 };
const valarray<size_t> len(lv, 2), str(dv, 2);
v0[gslice(3, len, str)] = v1;
// v0 == valarray<char>("abcAeBgCijDlEnFp", 16)
— end example ]
valarray operator[](const valarray<bool>& boolarr) const;
6
Returns: A valarray containing those elements of the controlled sequence designated by boolarr.
[ Example:
const valarray<char> v0("abcdefghijklmnop", 16);
const bool vb[] = { false, false, true, true, false, true };
// v0[valarray<bool>(vb, 6)] returns
// valarray<char>("cdf", 3)
— end example ]
§ 29.7.2.5
993
mask_array<T> operator[](const valarray<bool>& boolarr);
7
Returns: An object that holds references to elements of the controlled sequence selected by boolarr.
[ Example:
valarray<char> v0("abcdefghijklmnop", 16);
valarray<char> v1("ABC", 3);
const bool vb[] = { false, false, true, true, false, true };
v0[valarray<bool>(vb, 6)] = v1;
// v0 == valarray<char>("abABeCghijklmnop", 16)
— end example ]
valarray operator[](const valarray<size_t>& indarr) const;
8
Returns: A valarray containing those elements of the controlled sequence designated by indarr.
[ Example:
const valarray<char> v0("abcdefghijklmnop", 16);
const size_t vi[] = { 7, 5, 2, 3, 8 };
// v0[valarray<size_t>(vi, 5)] returns
// valarray<char>("hfcdi", 5)
— end example ]
indirect_array<T> operator[](const valarray<size_t>& indarr);
9
Returns: An object that holds references to elements of the controlled sequence selected by indarr.
[ Example:
valarray<char> v0("abcdefghijklmnop", 16);
valarray<char> v1("ABCDE", 5);
const size_t vi[] = { 7, 5, 2, 3, 8 };
v0[valarray<size_t>(vi, 5)] = v1;
// v0 == valarray<char>("abCDeBgAEjklmnop", 16)
— end example ]
29.7.2.6
valarray unary operators
[valarray.unary]
valarray operator+() const;
valarray operator-() const;
valarray operator~() const;
valarray<bool> operator!() const;
1
Requires: Each of these operators may only be instantiated for a type T to which the indicated operator
can be applied and for which the indicated operator returns a value which is of type T (bool for
operator!) or which may be unambiguously implicitly converted to type T (bool for operator!).
2
Returns: A valarray whose length is size(). Each element of the returned array is initialized with
the result of applying the indicated operator to the corresponding element of the array.
29.7.2.7
valarray compound assignment
[valarray.cassign]
valarray& operator*= (const valarray& v);
valarray& operator/= (const valarray& v);
valarray& operator%= (const valarray& v);
valarray& operator+= (const valarray& v);
valarray& operator-= (const valarray& v);
valarray& operator^= (const valarray& v);
valarray& operator&= (const valarray& v);
valarray& operator|= (const valarray& v);
valarray& operator<<=(const valarray& v);
valarray& operator>>=(const valarray& v);
1
Requires: size() == v.size(). Each of these operators may only be instantiated for a type T if the
indicated operator can be applied to two operands of type T. The value of an element in the left-hand
side of a valarray compound assignment operator does not depend on the value of another element in
that left hand side.
§ 29.7.2.7
994
2
Effects: Each of these operators performs the indicated operation on each of the elements of *this and
the corresponding element of v.
3
Returns: *this.
4
Remarks: The appearance of an array on the left-hand side of a compound assignment does not
invalidate references or pointers.
valarray& operator*= (const T& v);
valarray& operator/= (const T& v);
valarray& operator%= (const T& v);
valarray& operator+= (const T& v);
valarray& operator-= (const T& v);
valarray& operator^= (const T& v);
valarray& operator&= (const T& v);
valarray& operator|= (const T& v);
valarray& operator<<=(const T& v);
valarray& operator>>=(const T& v);
5
Requires: Each of these operators may only be instantiated for a type T if the indicated operator can
be applied to two operands of type T.
6
Effects: Each of these operators applies the indicated operation to each element of *this and v.
7
Returns: *this
8
Remarks: The appearance of an array on the left-hand side of a compound assignment does not
invalidate references or pointers to the elements of the array.
29.7.2.8
valarray member functions
[valarray.members]
void swap(valarray& v) noexcept;
1
Effects: *this obtains the value of v. v obtains the value of *this.
2
Complexity: Constant.
size_t size() const;
3
Returns: The number of elements in the array.
4
Complexity: Constant time.
T sum() const;
5
Requires: size() > 0. This function may only be instantiated for a type T to which operator+= can
be applied.
6
Returns: The sum of all the elements of the array. If the array has length 1, returns the value of
element 0. Otherwise, the returned value is calculated by applying operator+= to a copy of an element
of the array and all other elements of the array in an unspecified order.
T min() const;
7
Requires: size() > 0
8
Returns: The minimum value contained in *this. For an array of length 1, the value of element 0 is
returned. For all other array lengths, the determination is made using operator<.
T max() const;
9
Requires: size() > 0.
10
Returns: The maximum value contained in *this. For an array of length 1, the value of element 0 is
returned. For all other array lengths, the determination is made using operator<.
valarray shift(int n) const;
11
Returns: A valarray of length size(), each of whose elements I is (*this)[I
+ n] if I
+ n is
non-negative and less than size(), otherwise T().
[Note: If element zero is taken as the leftmost
element, a positive value of n shifts the elements left n places, with zero fill.
— end note ]
§ 29.7.2.8
995
12
[Example: If the argument has the value -2, the first two elements of the result will be value-
initialized (11.6); the third element of the result will be assigned the value of the first element of the
argument; etc.
— end example ]
valarray cshift(int n) const;
13
Returns: A valarray of length size() that is a circular shift of *this. If element zero is taken as the
leftmost element, a non-negative value of n shifts the elements circularly left n places and a negative
value of n shifts the elements circularly right −n places.
valarray apply(T func(T)) const;
valarray apply(T func(const T&)) const;
14
Returns: A valarray whose length is size(). Each element of the returned array is assigned the value
returned by applying the argument function to the corresponding element of *this.
void resize(size_t sz, T c = T());
15
Effects: Changes the length of the *this array to sz and then assigns to each element the value of the
second argument. Resizing invalidates all pointers and references to elements in the array.
29.7.3
valarray non-member operations
[valarray.nonmembers]
29.7.3.1
valarray binary operators
[valarray.binary]
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>&);
1
Requires: Each of these operators may only be instantiated for a type T to which the indicated operator
can be applied and for which the indicated operator returns a value which is of type T or which can be
unambiguously implicitly converted to type T. The argument arrays have the same length.
2
Returns: A valarray whose length is equal to the lengths of the argument arrays. Each element of the
returned array is initialized with the result of applying the indicated operator to the corresponding
elements of the argument arrays.
template<class T> valarray<T> operator* (const valarray<T>&, const T&);
template<class T> valarray<T> operator* (const T&, const valarray<T>&);
template<class T> valarray<T> operator/ (const valarray<T>&, const T&);
template<class T> valarray<T> operator/ (const T&, const valarray<T>&);
template<class T> valarray<T> operator% (const valarray<T>&, const T&);
template<class T> valarray<T> operator% (const T&, const valarray<T>&);
template<class T> valarray<T> operator+ (const valarray<T>&, const T&);
template<class T> valarray<T> operator+ (const T&, const valarray<T>&);
template<class T> valarray<T> operator- (const valarray<T>&, const T&);
template<class T> valarray<T> operator- (const T&, const valarray<T>&);
template<class T> valarray<T> operator^ (const valarray<T>&, const T&);
template<class T> valarray<T> operator^ (const T&, const valarray<T>&);
template<class T> valarray<T> operator& (const valarray<T>&, const T&);
template<class T> valarray<T> operator& (const T&, const valarray<T>&);
template<class T> valarray<T> operator| (const valarray<T>&, const T&);
template<class T> valarray<T> operator| (const T&, const valarray<T>&);
template<class T> valarray<T> operator<<(const valarray<T>&, const T&);
template<class T> valarray<T> operator<<(const T&, const valarray<T>&);
template<class T> valarray<T> operator>>(const valarray<T>&, const T&);
template<class T> valarray<T> operator>>(const T&, const valarray<T>&);
3
Requires: Each of these operators may only be instantiated for a type T to which the indicated operator
can be applied and for which the indicated operator returns a value which is of type T or which can be
unambiguously implicitly converted to type T.
§ 29.7.3.1
996
4
Returns: A valarray whose length is equal to the length of the array argument. Each element of the
returned array is initialized with the result of applying the indicated operator to the corresponding
element of the array argument and the non-array argument.
29.7.3.2
valarray logical operators
[valarray.comparison]
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 valarray<T>&);
1
Requires: Each of these operators may only be instantiated for a type T to which the indicated operator
can be applied and for which the indicated operator returns a value which is of type bool or which can
be unambiguously implicitly converted to type bool. The two array arguments have the same length.
2
Returns: A valarray<bool> whose length is equal to the length of the array arguments. Each element
of the returned array is initialized with the result of applying the indicated operator to the corresponding
elements of the argument arrays.
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 valarray<T>&, const T&);
template<class T> valarray<bool> operator!=(const T&, const valarray<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 valarray<T>&, const T&);
template<class T> valarray<bool> operator> (const T&, const valarray<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 valarray<T>&, const T&);
template<class T> valarray<bool> operator>=(const T&, const valarray<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 valarray<T>&, const T&);
template<class T> valarray<bool> operator||(const T&, const valarray<T>&);
3
Requires: Each of these operators may only be instantiated for a type T to which the indicated operator
can be applied and for which the indicated operator returns a value which is of type bool or which can
be unambiguously implicitly converted to type bool.
4
Returns: A valarray<bool> whose length is equal to the length of the array argument. Each element of
the returned array is initialized with the result of applying the indicated operator to the corresponding
element of the array and the non-array argument.
29.7.3.3
valarray transcendentals
[valarray.transcend]
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> 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> 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> 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>&);
template<class T> valarray<T> sin
(const valarray<T>&);
§ 29.7.3.3
997
template<class T> valarray<T> sinh (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>&);
1
Requires: Each of these functions may only be instantiated for a type T to which a unique function
with the indicated name can be applied (unqualified). This function shall return a value which is of
type T or which can be unambiguously implicitly converted to type T.
29.7.3.4
valarray specialized algorithms
[valarray.special]
template<class T> void swap(valarray<T>& x, valarray<T>& y) noexcept;
1
Effects: Equivalent to x.swap(y).
29.7.4
Class slice
[class.slice]
29.7.4.1
Class slice overview
[class.slice.overview]
namespace std {
class slice {
public:
slice();
slice(size_t, size_t, size_t);
size_t start() const;
size_t size() const;
size_t stride() const;
};
}
1
The slice class represents a BLAS-like slice from an array. Such a slice is specified by a starting index, a
length, and a stride.283
29.7.4.2
slice constructors
[cons.slice]
slice();
slice(size_t start, size_t length, size_t stride);
slice(const slice&);
1
The default constructor is equivalent to slice(0, 0, 0). A default constructor is provided only to
permit the declaration of arrays of slices. The constructor with arguments for a slice takes a start,
length, and stride parameter.
2
[Example: slice(3, 8, 2) constructs a slice which selects elements 3, 5, 7, ...
17 from an array.
— end example ]
29.7.4.3
slice access functions
[slice.access]
size_t start() const;
size_t size() const;
size_t stride() const;
1
Returns: The start, length, or stride specified by a slice object.
2
Complexity: Constant time.
29.7.5
Class template slice_array
[template.slice.array]
29.7.5.1
Class template slice_array overview
[template.slice.array.overview]
namespace std {
template<class T> class slice_array {
public:
using value_type = T;
283) BLAS stands for Basic Linear Algebra Subprograms. C++ programs may instantiate this class. See, for example, Dongarra,
Du Croz, Duff, and Hammerling: A set of Level 3 Basic Linear Algebra Subprograms; Technical Report MCS-P1-0888, Argonne
National Laboratory (USA), Mathematics and Computer Science Division, August, 1988.
§ 29.7.5.1
998
void operator=
(const valarray<T>&) const;
void operator*= (const valarray<T>&) const;
void operator/= (const valarray<T>&) const;
void operator%= (const valarray<T>&) const;
void operator+= (const valarray<T>&) const;
void operator-= (const valarray<T>&) const;
void operator^= (const valarray<T>&) const;
void operator&= (const valarray<T>&) const;
void operator|= (const valarray<T>&) const;
void operator<<=(const valarray<T>&) const;
void operator>>=(const valarray<T>&) const;
slice_array(const slice_array&);
~slice_array();
const slice_array& operator=(const slice_array&) const;
void operator=(const T&) const;
slice_array() = delete;
// as implied by declaring copy constructor above
};
}
1
The slice_array template is a helper template used by the slice subscript operator
slice_array<T> valarray<T>::operator[](slice);
It has reference semantics to a subset of an array specified by a slice object.
2
[ Example: The expression a[slice(1, 5, 3)] = b; has the effect of assigning the elements of b to a slice
of the elements in a. For the slice shown, the elements selected from a are 1, 4, ..., 13.
— end example ]
29.7.5.2
slice_array assignment
[slice.arr.assign]
void operator=(const valarray<T>&) const;
const slice_array& operator=(const slice_array&) const;
1
These assignment operators have reference semantics, assigning the values of the argument array
elements to selected elements of the valarray<T> object to which the slice_array object refers.
29.7.5.3
slice_array compound assignment
[slice.arr.comp.assign]
void operator*= (const valarray<T>&) const;
void operator/= (const valarray<T>&) const;
void operator%= (const valarray<T>&) const;
void operator+= (const valarray<T>&) const;
void operator-= (const valarray<T>&) const;
void operator^= (const valarray<T>&) const;
void operator&= (const valarray<T>&) const;
void operator|= (const valarray<T>&) const;
void operator<<=(const valarray<T>&) const;
void operator>>=(const valarray<T>&) const;
1
These compound assignments have reference semantics, applying the indicated operation to the elements
of the argument array and selected elements of the valarray<T> object to which the slice_array
object refers.
29.7.5.4
slice_array fill function
[slice.arr.fill]
void operator=(const T&) const;
1
This function has reference semantics, assigning the value of its argument to the elements of the
valarray<T> object to which the slice_array object refers.
29.7.6
The gslice class
[class.gslice]
29.7.6.1
The gslice class overview
[class.gslice.overview]
namespace std {
class gslice {
public:
gslice();
§ 29.7.6.1
999
gslice(size_t s, const valarray<size_t>& l, const valarray<size_t>& d);
size_t
start() const;
valarray<size_t> size() const;
valarray<size_t> stride() const;
};
}
1
This class represents a generalized slice out of an array. A gslice is defined by a starting offset (s), a set of
lengths (lj ), and a set of strides (dj ). The number of lengths shall equal the number of strides.
2
A gslice represents a mapping from a set of indices (ij ), equal in number to the number of strides, to a
single index k. It is useful for building multidimensional array classes using the valarray template, which is
one-dimensional. The set of one-dimensional index values specified by a gslice are
k=s+ ijdj
j
where the multidimensional indices ij range in value from 0 to lij − 1.
3
[ Example: The gslice specification
start
= 3
length = {2, 4, 3}
stride = {19, 4, 1}
yields the sequence of one-dimensional indices
k = 3 + (0,1) × 19 + (0,1,2,3) × 4 + (0,1,2) × 1
which are ordered as shown in the following table:
(i0, i1, i2, k)
=
(0,
0,
0,
3),
(0,
0,
1,
4),
(0,
0,
2,
5),
(0,
1,
0,
7),
(0,
1,
1,
8),
(0,
1,
2,
9),
(0,
2,
0,
11),
(0,
2,
1,
12),
(0,
2,
2,
13),
(0,
3,
0,
15),
(0,
3,
1,
16),
(0,
3,
2,
17),
(1,
0,
0,
22),
(1,
0,
1,
23),
(1,
3,
2,
36)
That is, the highest-ordered index turns fastest.
— end example ]
4
It is possible to have degenerate generalized slices in which an address is repeated.
5
[ Example: If the stride parameters in the previous example are changed to {1, 1, 1}, the first few elements of
the resulting sequence of indices will be
(0,
0,
0,
3),
(0,
0,
1,
4),
(0,
0,
2,
5),
(0,
1,
0,
4),
(0,
1,
1,
5),
(0,
1,
2,
6),
§ 29.7.6.1
1000
— end example ]
6
If a degenerate slice is used as the argument to the non-const version of operator[](const gslice&), the
behavior is undefined.
29.7.6.2
gslice constructors
[gslice.cons]
gslice();
gslice(size_t start, const valarray<size_t>& lengths,
const valarray<size_t>& strides);
gslice(const gslice&);
1
The default constructor is equivalent to gslice(0, valarray<size_t>(), valarray<size_t>()).
The constructor with arguments builds a gslice based on a specification of start, lengths, and strides,
as explained in the previous subclause.
29.7.6.3
gslice access functions
[gslice.access]
size_t
start() const;
valarray<size_t> size() const;
valarray<size_t> stride() const;
1
Returns: The representation of the start, lengths, or strides specified for the gslice.
2
Complexity: start() is constant time. size() and stride() are linear in the number of strides.
29.7.7
Class template gslice_array
[template.gslice.array]
29.7.7.1
Class template gslice_array overview
[template.gslice.array.overview]
namespace std {
template<class T> class gslice_array {
public:
using value_type = T;
void operator=
(const valarray<T>&) const;
void operator*= (const valarray<T>&) const;
void operator/= (const valarray<T>&) const;
void operator%= (const valarray<T>&) const;
void operator+= (const valarray<T>&) const;
void operator-= (const valarray<T>&) const;
void operator^= (const valarray<T>&) const;
void operator&= (const valarray<T>&) const;
void operator|= (const valarray<T>&) const;
void operator<<=(const valarray<T>&) const;
void operator>>=(const valarray<T>&) const;
gslice_array(const gslice_array&);
~gslice_array();
const gslice_array& operator=(const gslice_array&) const;
void operator=(const T&) const;
gslice_array() = delete;
// as implied by declaring copy constructor above
};
}
1
This template is a helper template used by the slice subscript operator
gslice_array<T> valarray<T>::operator[](const gslice&);
2
It has reference semantics to a subset of an array specified by a gslice object.
3
Thus, the expression a[gslice(1, length, stride)] = b has the effect of assigning the elements of
b to a generalized slice of the elements in a.
29.7.7.2
gslice_array assignment
[gslice.array.assign]
void operator=(const valarray<T>&) const;
§ 29.7.7.2
1001
const gslice_array& operator=(const gslice_array&) const;
1
These assignment operators have reference semantics, assigning the values of the argument array
elements to selected elements of the valarray<T> object to which the gslice_array refers.
29.7.7.3
gslice_array compound assignment
[gslice.array.comp.assign]
void operator*= (const valarray<T>&) const;
void operator/= (const valarray<T>&) const;
void operator%= (const valarray<T>&) const;
void operator+= (const valarray<T>&) const;
void operator-= (const valarray<T>&) const;
void operator^= (const valarray<T>&) const;
void operator&= (const valarray<T>&) const;
void operator|= (const valarray<T>&) const;
void operator<<=(const valarray<T>&) const;
void operator>>=(const valarray<T>&) const;
1
These compound assignments have reference semantics, applying the indicated operation to the elements
of the argument array and selected elements of the valarray<T> object to which the gslice_array
object refers.
29.7.7.4
gslice_array fill function
[gslice.array.fill]
void operator=(const T&) const;
1
This function has reference semantics, assigning the value of its argument to the elements of the
valarray<T> object to which the gslice_array object refers.
29.7.8
Class template mask_array
[template.mask.array]
29.7.8.1
Class template mask_array overview
[template.mask.array.overview]
namespace std {
template<class T> class mask_array {
public:
using value_type = T;
void operator=
(const valarray<T>&) const;
void operator*= (const valarray<T>&) const;
void operator/= (const valarray<T>&) const;
void operator%= (const valarray<T>&) const;
void operator+= (const valarray<T>&) const;
void operator-= (const valarray<T>&) const;
void operator^= (const valarray<T>&) const;
void operator&= (const valarray<T>&) const;
void operator|= (const valarray<T>&) const;
void operator<<=(const valarray<T>&) const;
void operator>>=(const valarray<T>&) const;
mask_array(const mask_array&);
~mask_array();
const mask_array& operator=(const mask_array&) const;
void operator=(const T&) const;
mask_array() = delete;
// as implied by declaring copy constructor above
};
}
1
This template is a helper template used by the mask subscript operator:
mask_array<T> valarray<T>::operator[](const valarray<bool>&).
2
It has reference semantics to a subset of an array specified by a boolean mask. Thus, the expression
a[mask] = b; has the effect of assigning the elements of b to the masked elements in a (those for
which the corresponding element in mask is true.)
§ 29.7.8.1
1002
29.7.8.2
mask_array assignment
[mask.array.assign]
void operator=(const valarray<T>&) const;
const mask_array& operator=(const mask_array&) const;
1
These assignment operators have reference semantics, assigning the values of the argument array
elements to selected elements of the valarray<T> object to which it refers.
29.7.8.3
mask_array compound assignment
[mask.array.comp.assign]
void operator*= (const valarray<T>&) const;
void operator/= (const valarray<T>&) const;
void operator%= (const valarray<T>&) const;
void operator+= (const valarray<T>&) const;
void operator-= (const valarray<T>&) const;
void operator^= (const valarray<T>&) const;
void operator&= (const valarray<T>&) const;
void operator|= (const valarray<T>&) const;
void operator<<=(const valarray<T>&) const;
void operator>>=(const valarray<T>&) const;
1
These compound assignments have reference semantics, applying the indicated operation to the elements
of the argument array and selected elements of the valarray<T> object to which the mask object refers.
29.7.8.4
mask_array fill function
[mask.array.fill]
void operator=(const T&) const;
1
This function has reference semantics, assigning the value of its argument to the elements of the
valarray<T> object to which the mask_array object refers.
29.7.9
Class template indirect_array
[template.indirect.array]
29.7.9.1
Class template indirect_array overview
[template.indirect.array.overview]
namespace std {
template<class T> class indirect_array {
public:
using value_type = T;
void operator=
(const valarray<T>&) const;
void operator*= (const valarray<T>&) const;
void operator/= (const valarray<T>&) const;
void operator%= (const valarray<T>&) const;
void operator+= (const valarray<T>&) const;
void operator-= (const valarray<T>&) const;
void operator^= (const valarray<T>&) const;
void operator&= (const valarray<T>&) const;
void operator|= (const valarray<T>&) const;
void operator<<=(const valarray<T>&) const;
void operator>>=(const valarray<T>&) const;
indirect_array(const indirect_array&);
~indirect_array();
const indirect_array& operator=(const indirect_array&) const;
void operator=(const T&) const;
indirect_array() = delete;
// as implied by declaring copy constructor above
};
}
1
This template is a helper template used by the indirect subscript operator
indirect_array<T> valarray<T>::operator[](const valarray<size_t>&).
2
It has reference semantics to a subset of an array specified by an indirect_array. Thus the expression
a[indirect] = b; has the effect of assigning the elements of b to the elements in a whose indices
appear in indirect.
§ 29.7.9.1
1003
29.7.9.2
indirect_array assignment
[indirect.array.assign]
void operator=(const valarray<T>&) const;
const indirect_array& operator=(const indirect_array&) const;
1
These assignment operators have reference semantics, assigning the values of the argument array
elements to selected elements of the valarray<T> object to which it refers.
2
If the indirect_array specifies an element in the valarray<T> object to which it refers more than
once, the behavior is undefined.
3
[ Example:
int addr[] = {2, 3, 1, 4, 4};
valarray<size_t> indirect(addr, 5);
valarray<double> a(0., 10), b(1., 5);
a[indirect] = b;
results in undefined behavior since element 4 is specified twice in the indirection.
— end example ]
29.7.9.3
indirect_array compound assignment
[indirect.array.comp.assign]
void operator*= (const valarray<T>&) const;
void operator/= (const valarray<T>&) const;
void operator%= (const valarray<T>&) const;
void operator+= (const valarray<T>&) const;
void operator-= (const valarray<T>&) const;
void operator^= (const valarray<T>&) const;
void operator&= (const valarray<T>&) const;
void operator|= (const valarray<T>&) const;
void operator<<=(const valarray<T>&) const;
void operator>>=(const valarray<T>&) const;
1
These compound assignments have reference semantics, applying the indicated operation to the elements
of the argument array and selected elements of the valarray<T> object to which the indirect_array
object refers.
2
If the indirect_array specifies an element in the valarray<T> object to which it refers more than
once, the behavior is undefined.
29.7.9.4
indirect_array fill function
[indirect.array.fill]
void operator=(const T&) const;
1
This function has reference semantics, assigning the value of its argument to the elements of the
valarray<T> object to which the indirect_array object refers.
29.7.10
valarray range access
[valarray.range]
1
In the begin and end function templates that follow, unspecified 1 is a type that meets the requirements
of a mutable random access iterator (27.2.7) and of a contiguous iterator (27.2.1) whose value_type is the
template parameter T and whose reference type is T&. unspecified 2 is a type that meets the requirements
of a constant random access iterator (27.2.7) and of a contiguous iterator (27.2.1) whose value_type is the
template parameter T and whose reference type is const T&.
2
The iterators returned by begin and end for an array are guaranteed to be valid until the member function
resize(size_t, T) (29.7.2.8) is called for that array or until the lifetime of that array ends, whichever
happens first.
template<class T> unspecified 1 begin(valarray<T>& v);
template<class T> unspecified 2 begin(const valarray<T>& v);
3
Returns: An iterator referencing the first value in the array.
template<class T> unspecified 1 end(valarray<T>& v);
template<class T> unspecified 2 end(const valarray<T>& v);
4
Returns: An iterator referencing one past the last value in the array.
§ 29.7.10
1004
29.8
Generalized numeric operations
[numeric.ops]
29.8.1
Header <numeric> synopsis
[numeric.ops.overview]
namespace std {
// 29.8.2, accumulate
template<class InputIterator, class T>
T accumulate(InputIterator first, InputIterator last, T init);
template<class InputIterator, class T, class BinaryOperation>
T accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);
// 29.8.3, reduce
template<class InputIterator>
typename iterator_traits<InputIterator>::value_type
reduce(InputIterator first, InputIterator last);
template<class InputIterator, class T>
T reduce(InputIterator first, InputIterator last, T init);
template<class InputIterator, class T, class BinaryOperation>
T reduce(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);
template<class ExecutionPolicy, class ForwardIterator>
typename iterator_traits<ForwardIterator>::value_type
reduce(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator first, ForwardIterator last);
template<class ExecutionPolicy, class ForwardIterator, class T>
T reduce(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator first, ForwardIterator last, T init);
template<class ExecutionPolicy, class ForwardIterator, class T, class BinaryOperation>
T reduce(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator first, ForwardIterator last, T init, BinaryOperation
binary_op);
// 29.8.4, inner product
template<class InputIterator1, class InputIterator2, class T>
T inner_product(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, T init);
template<class InputIterator1, class InputIterator2, class T,
class BinaryOperation1, class BinaryOperation2>
T inner_product(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, T init,
BinaryOperation1 binary_op1,
BinaryOperation2 binary_op2);
// 29.8.5, transform reduce
template<class InputIterator1, class InputIterator2, class T>
T transform_reduce(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2,
T init);
template<class InputIterator1, class InputIterator2, class T,
class BinaryOperation1, class BinaryOperation2>
T transform_reduce(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2,
T init,
BinaryOperation1 binary_op1,
BinaryOperation2 binary_op2);
template<class InputIterator, class T,
class BinaryOperation, class UnaryOperation>
T transform_reduce(InputIterator first, InputIterator last,
T init,
BinaryOperation binary_op, UnaryOperation unary_op);
template<class ExecutionPolicy,
class ForwardIterator1, class ForwardIterator2, class T>
T transform_reduce(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2,
T init);
§ 29.8.1
1005
template<class ExecutionPolicy,
class ForwardIterator1, class ForwardIterator2, class T,
class BinaryOperation1, class BinaryOperation2>
T transform_reduce(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2,
T init,
BinaryOperation1 binary_op1,
BinaryOperation2 binary_op2);
template<class ExecutionPolicy,
class ForwardIterator, class T,
class BinaryOperation, class UnaryOperation>
T transform_reduce(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator first, ForwardIterator last,
T init,
BinaryOperation binary_op, UnaryOperation unary_op);
// 29.8.6, partial sum
template<class InputIterator, class OutputIterator>
OutputIterator partial_sum(InputIterator first,
InputIterator last,
OutputIterator result);
template<class InputIterator, class OutputIterator, class BinaryOperation>
OutputIterator partial_sum(InputIterator first,
InputIterator last,
OutputIterator result,
BinaryOperation binary_op);
// 29.8.7, exclusive scan
template<class InputIterator, class OutputIterator, class T>
OutputIterator exclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
T init);
template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
OutputIterator exclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
T init, BinaryOperation binary_op);
template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class
T>
ForwardIterator2 exclusive_scan(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator1 first, ForwardIterator1 last,
ForwardIterator2 result,
T init);
template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class
T,
class BinaryOperation>
ForwardIterator2 exclusive_scan(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator1 first, ForwardIterator1 last,
ForwardIterator2 result,
T init, BinaryOperation binary_op);
// 29.8.8, inclusive scan
template<class InputIterator, class OutputIterator>
OutputIterator inclusive_scan(InputIterator first, InputIterator last,
OutputIterator result);
template<class InputIterator, class OutputIterator, class BinaryOperation>
OutputIterator inclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
BinaryOperation binary_op);
template<class InputIterator, class OutputIterator, class BinaryOperation, class T>
OutputIterator inclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
BinaryOperation binary_op, T init);
template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2>
ForwardIterator2 inclusive_scan(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator1 first, ForwardIterator1 last,
§
29.8.1
1006
ForwardIterator2 result);
template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
class BinaryOperation>
ForwardIterator2 inclusive_scan(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator1 first, ForwardIterator1 last,
ForwardIterator2 result,
BinaryOperation binary_op);
template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
class BinaryOperation, class T>
ForwardIterator2 inclusive_scan(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator1 first, ForwardIterator1 last,
ForwardIterator2 result,
BinaryOperation binary_op, T init);
// 29.8.9, transform exclusive scan
template<class InputIterator, class OutputIterator, class T,
class BinaryOperation, class UnaryOperation>
OutputIterator transform_exclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
T init,
BinaryOperation binary_op,
UnaryOperation unary_op);
template<class ExecutionPolicy,
class ForwardIterator1, class ForwardIterator2, class T,
class BinaryOperation, class UnaryOperation>
ForwardIterator2 transform_exclusive_scan(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator1 first, ForwardIterator1
last,
ForwardIterator2 result,
T init,
BinaryOperation binary_op,
UnaryOperation unary_op);
// 29.8.10, transform inclusive scan
template<class InputIterator, class OutputIterator,
class BinaryOperation, class UnaryOperation>
OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
BinaryOperation binary_op,
UnaryOperation unary_op);
template<class InputIterator, class OutputIterator,
class BinaryOperation, class UnaryOperation, class T>
OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
BinaryOperation binary_op,
UnaryOperation unary_op,
T init);
template<class ExecutionPolicy,
class ForwardIterator1, class ForwardIterator2,
class BinaryOperation, class UnaryOperation>
ForwardIterator2 transform_inclusive_scan(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator1 first, ForwardIterator1
last,
ForwardIterator2 result,
BinaryOperation binary_op,
UnaryOperation unary_op);
template<class ExecutionPolicy,
class ForwardIterator1, class ForwardIterator2,
class BinaryOperation, class UnaryOperation, class T>
ForwardIterator2 transform_inclusive_scan(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator1 first, ForwardIterator1
last,
ForwardIterator2 result,
BinaryOperation binary_op,
UnaryOperation unary_op,
T init);
§
29.8.1
1007
// 29.8.11, adjacent difference
template<class InputIterator, class OutputIterator>
OutputIterator adjacent_difference(InputIterator first,
InputIterator last,
OutputIterator result);
template<class InputIterator, class OutputIterator, class BinaryOperation>
OutputIterator adjacent_difference(InputIterator first,
InputIterator last,
OutputIterator result,
BinaryOperation binary_op);
template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2>
ForwardIterator2 adjacent_difference(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator1 first,
ForwardIterator1 last,
ForwardIterator2 result);
template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
class BinaryOperation>
ForwardIterator2 adjacent_difference(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator1 first,
ForwardIterator1 last,
ForwardIterator2 result,
BinaryOperation binary_op);
// 29.8.12, iota
template<class ForwardIterator, class T>
void iota(ForwardIterator first, ForwardIterator last, T value);
// 29.8.13, greatest common divisor
template<class M, class N>
constexpr common_type_t<M,N> gcd(M m, N n);
// 29.8.14, least common multiple
template<class M, class N>
constexpr common_type_t<M,N> lcm(M m, N n);
}
1
The requirements on the types of algorithms’ arguments that are described in the introduction to Clause
28
also apply to the following algorithms.
2
Throughout this subclause, the parameters UnaryOperation, BinaryOperation, BinaryOperation1, and
BinaryOperation2 are used whenever an algorithm expects a function object (23.14).
3
[ Note: The use of closed ranges as well as semi-open ranges to specify requirements throughout this subclause
is intentional.
— end note ]
29.8.2
Accumulate
[accumulate]
template<class InputIterator, class T>
T accumulate(InputIterator first, InputIterator last, T init);
template<class InputIterator, class T, class BinaryOperation>
T accumulate(InputIterator first, InputIterator last, T init,
BinaryOperation binary_op);
1
Requires: T shall meet the requirements of CopyConstructible (Table 24) and CopyAssignable
(Table 26) types. In the range [first, last], binary_op shall neither modify elements nor invalidate
iterators or subranges.284
2
Effects: Computes its result by initializing the accumulator acc with the initial value init and then
modifies it with acc = std::move(acc) + *i or acc = binary_op(std::move(acc), *i) for every
iterator i in the range [first, last) in order.285
284) The use of fully closed ranges is intentional.
285) accumulate is similar to the APL reduction operator and Common Lisp reduce function, but it avoids the difficulty of
defining the result of reduction on an empty sequence by always requiring an initial value.
§ 29.8.2
1008
29.8.3
Reduce
[reduce]
template<class InputIterator>
typename iterator_traits<InputIterator>::value_type
reduce(InputIterator first, InputIterator last);
1
Effects: Equivalent to:
return reduce(first, last,
typename iterator_traits<InputIterator>::value_type{});
template<class ExecutionPolicy, class ForwardIterator>
typename iterator_traits<ForwardIterator>::value_type
reduce(ExecutionPolicy&& exec,
ForwardIterator first, ForwardIterator last);
2
Effects: Equivalent to:
return reduce(std::forward<ExecutionPolicy>(exec), first, last,
typename iterator_traits<ForwardIterator>::value_type{});
template<class InputIterator, class T>
T reduce(InputIterator first, InputIterator last, T init);
3
Effects: Equivalent to:
return reduce(first, last, init, plus<>());
template<class ExecutionPolicy, class ForwardIterator, class T>
T reduce(ExecutionPolicy&& exec,
ForwardIterator first, ForwardIterator last, T init);
4
Effects: Equivalent to:
return reduce(std::forward<ExecutionPolicy>(exec), first, last, init, plus<>());
template<class InputIterator, class T, class BinaryOperation>
T reduce(InputIterator first, InputIterator last, T init,
BinaryOperation binary_op);
template<class ExecutionPolicy, class ForwardIterator, class T, class BinaryOperation>
T reduce(ExecutionPolicy&& exec,
ForwardIterator first, ForwardIterator last, T init,
BinaryOperation binary_op);
5
Requires:
(5.1)
T shall be MoveConstructible (Table 23).
(5.2)
All of binary_op(init, *first), binary_op(*first, init), binary_op(init,
init), and
binary_op(*first, *first) shall be convertible to T.
(5.3)
binary_op shall neither invalidate iterators or subranges, nor modify elements in the range
[first, last].
6
Returns: GENERALIZED_SUM(binary_op, init, *i, ...) for every i in [first, last).
7
Complexity: O(last - first) applications of binary_op.
8
[Note: The difference between reduce and accumulate is that reduce applies binary_op in an
unspecified order, which yields a nondeterministic result for non-associative or non-commutative
binary_op such as floating-point addition.
— end note ]
29.8.4
Inner product
[inner.product]
template<class InputIterator1, class InputIterator2, class T>
T inner_product(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, T init);
template<class InputIterator1, class InputIterator2, class T,
class BinaryOperation1, class BinaryOperation2>
T inner_product(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, T init,
BinaryOperation1 binary_op1,
§ 29.8.4
1009
BinaryOperation2 binary_op2);
1
Requires: T shall meet the requirements of CopyConstructible (Table 24) and CopyAssignable (Ta-
ble 26) types. In the ranges [first1, last1] and [first2, first2 + (last1 - first1)] binary_-
op1 and binary_op2 shall neither modify elements nor invalidate iterators or subranges.286
2
Effects: Computes its result by initializing the accumulator acc with the initial value init and then
modifying it with acc = std::move(acc) + (*i1) * (*i2) or acc = binary_op1(std::move(acc),
binary_op2(*i1, *i2)) for every iterator i1 in the range [first1, last1) and iterator i2 in the
range [first2, first2 + (last1 - first1)) in order.
29.8.5
Transform reduce
[transform.reduce]
template<class InputIterator1, class InputIterator2, class T>
T transform_reduce(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2,
T init);
template<class ExecutionPolicy,
class ForwardIterator1, class ForwardIterator2, class T>
T transform_reduce(ExecutionPolicy&& exec,
ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2,
T init);
1
Effects: Equivalent to:
return transform_reduce(first1, last1, first2, init, plus<>(),
multiplies<>());
template<class InputIterator1, class InputIterator2, class T,
class BinaryOperation1, class BinaryOperation2>
T transform_reduce(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2,
T init,
BinaryOperation1 binary_op1,
BinaryOperation2 binary_op2);
template<class ExecutionPolicy,
class ForwardIterator1, class ForwardIterator2, class T,
class BinaryOperation1, class BinaryOperation2>
T transform_reduce(ExecutionPolicy&& exec,
ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2,
T init,
BinaryOperation1 binary_op1,
BinaryOperation2 binary_op2);
2
Requires:
(2.1)
T shall be MoveConstructible (Table 23).
(2.2)
All of
(2.2.1)
binary_op1(init, init),
(2.2.2)
binary_op1(init, binary_op2(*first1, *first2)),
(2.2.3)
binary_op1(binary_op2(*first1, *first2), init), and
(2.2.4)
binary_op1(binary_op2(*first1, *first2), binary_op2(*first1, *first2))
shall be convertible to T.
(2.3)
Neither binary_op1 nor binary_op2 shall invalidate subranges, or modify elements in the ranges
[first1, last1] and [first2, first2 + (last1 - first1)].
3
Returns:
GENERALIZED_SUM(binary_op1, init, binary_op2(*i, *(first2 + (i - first1))), ...)
for every iterator i in [first1, last1).
4
Complexity: O(last1 - first1) applications each of binary_op1 and binary_op2.
286) The use of fully closed ranges is intentional.
§ 29.8.5
1010
template<class InputIterator, class T,
class BinaryOperation, class UnaryOperation>
T transform_reduce(InputIterator first, InputIterator last, T init,
BinaryOperation binary_op, UnaryOperation unary_op);
template<class ExecutionPolicy,
class ForwardIterator, class T,
class BinaryOperation, class UnaryOperation>
T transform_reduce(ExecutionPolicy&& exec,
ForwardIterator first, ForwardIterator last,
T init, BinaryOperation binary_op, UnaryOperation unary_op);
5
Requires:
(5.1)
T shall be MoveConstructible (Table 23).
(5.2)
All of
(5.2.1)
binary_op(init, init),
(5.2.2)
binary_op(init, unary_op(*first)),
(5.2.3)
binary_op(unary_op(*first), init), and
(5.2.4)
binary_op(unary_op(*first), unary_op(*first))
shall be convertible to T.
(5.3)
Neither unary_op nor binary_op shall invalidate subranges, or modify elements in the range
[first, last].
6
Returns:
GENERALIZED_SUM(binary_op, init, unary_op(*i), ...)
for every iterator i in [first, last).
7
Complexity: O(last - first) applications each of unary_op and binary_op.
8
[ Note: transform_reduce does not apply unary_op to init. — end note ]
29.8.6
Partial sum
[partial.sum]
template<class InputIterator, class OutputIterator>
OutputIterator partial_sum(
InputIterator first, InputIterator last,
OutputIterator result);
template<class InputIterator, class OutputIterator, class BinaryOperation>
OutputIterator partial_sum(
InputIterator first, InputIterator last,
OutputIterator result, BinaryOperation binary_op);
1
Requires: InputIterator’s value type shall be constructible from the type of *first. The result of the
expression std::move(acc) + *i or binary_op(std::move(acc), *i) shall be implicitly convertible
to InputIterator’s value type. acc shall be writable (27.2.1) to the result output iterator. In the
ranges [first, last] and [result, result + (last - first)] binary_op shall neither modify
elements nor invalidate iterators or subranges.287
2
Effects: For a non-empty range, the function creates an accumulator acc whose type is InputIterator’s
value type, initializes it with
*first, and assigns the result to *result. For every iterator i
in [first + 1, last) in order, acc is then modified by acc = std::move(acc) + *i or acc =
binary_op(std::move(acc), *i) and the result is assigned to *(result + (i - first)).
3
Returns: result + (last - first).
4
Complexity: Exactly (last - first) - 1 applications of the binary operation.
5
Remarks: result may be equal to first.
287) The use of fully closed ranges is intentional.
§ 29.8.6
1011

 

 

 

 

 

 

 

Content      ..     32      33      34      35     ..