<numeric> 函数

accumulate

通过计算连续部分和来计算指定范围内所有元素的总和,包括一些初始值。 或者,计算指定二元运算的连续部分结果的结果。

template <class InputIterator, class Type>
Type accumulate(
    InputIterator first,
    InputIterator last,
    Type init);

template <class InputIterator, class Type, class BinaryOperation>
Type accumulate(
    InputIterator first,
    InputIterator last,
    Type init,
    BinaryOperation binary_op);

参数

first
输入迭代器,使用 binary_op 在要求和或合并的范围内寻址第一个元素

last
输入迭代器,使用 binary_op 在要求和或合并的范围内寻址最后一个元素,即迭代累计中实际包含的最后一个元素之外的一个位置

init
使用 binary_op 向其依次添加或合并每个元素的初始值

binary_op
要应用于指定范围内每个元素的二元运算及其上一应用的结果。

返回值

init 与指定范围内第一个模板函数的所有元素或第二个模板函数的所有元素的总和,将二元运算 binary_op 替代求和运算,应用于 (*PartialResult, in_iter) 的结果,其中 PartialResult 是该运算的上一应用的结果,in_iter 是指向范围内下一元素的迭代器

备注

初始值确保当范围为空时具有明确定义的结果,在这种情况下,将返回 init。 二元运算不需要具有关联性或可交换性。 将结果初始化为初始值 init,然后以迭代方式计算范围内的 result = binary_op(result, in_iter),其中 in_iter 是指向范围内每个连续元素的迭代器。 该范围必须有效,并且其复杂度与该范围的大小呈线性关系。 二元运算符的返回类型必须可转换为 Type,以确保在迭代期间闭包。

示例

// numeric_accum.cpp
// compile with: /EHsc
#include <vector>
#include <numeric>
#include <functional>
#include <iostream>

int main( )
{
   using namespace std;

   vector <int> v1, v2(20);
   vector <int>::iterator iter1, iter2;

   int i;
   for (i = 1; i < 21; i++)
   {
      v1.push_back(i);
   }

   cout << "The original vector v1 is:\n ( " ;
   for (iter1 = v1.begin(); iter1 != v1.end(); iter1++)
      cout << *iter1 << " ";
   cout << ")." << endl;

   // The first member function for the accumulated sum
   int total;
   total = accumulate(v1.begin(), v1.end(), 0);

   cout << "The sum of the integers from 1 to 20 is: "
        << total << "." << endl;

   // Constructing a vector of partial sums
   int j = 0, partotal;
   for (iter1 = v1.begin(); iter1 != v1.end(); iter1++)
   {
      partotal = accumulate(v1.begin(), iter1 + 1, 0);
      v2[j] = partotal;
      j++;
   }

   cout << "The vector of partial sums is:\n ( " ;
   for (iter2 = v2.begin(); iter2 != v2.end(); iter2++)
      cout << *iter2 << " ";
   cout << ")." << endl << endl;

   // The second member function for the accumulated product
   vector <int> v3, v4(10);
   vector <int>::iterator iter3, iter4;

   int s;
   for (s = 1; s < 11; s++)
   {
      v3.push_back(s);
   }

   cout << "The original vector v3 is:\n ( " ;
   for (iter3 = v3.begin(); iter3 != v3.end(); iter3++)
      cout << *iter3 << " ";
   cout << ")." << endl;

   int ptotal;
   ptotal = accumulate(v3.begin(), v3.end(), 1, multiplies<int>());

   cout << "The product of the integers from 1 to 10 is: "
        << ptotal << "." << endl;

   // Constructing a vector of partial products
   int k = 0, ppartotal;
   for (iter3 = v3.begin(); iter3 != v3.end(); iter3++) {
      ppartotal = accumulate(v3.begin(), iter3 + 1, 1, multiplies<int>());
      v4[k] = ppartotal;
      k++;
   }

   cout << "The vector of partial products is:\n ( " ;
   for (iter4 = v4.begin(); iter4 != v4.end(); iter4++)
      cout << *iter4 << " ";
   cout << ")." << endl;
}
The original vector v1 is:
( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ).
The sum of the integers from 1 to 20 is: 210.
The vector of partial sums is:
( 1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 210 ).

The original vector v3 is:
( 1 2 3 4 5 6 7 8 9 10 ).
The product of the integers from 1 to 10 is: 3628800.
The vector of partial products is:
( 1 2 6 24 120 720 5040 40320 362880 3628800 ).

adjacent_difference

计算输入范围中每个元素与其前一元素之间的连续差值。 将结果输出到目标范围。 或者,计算将差值运算替换为其他指定二元运算的一般化程序的结果。

template <class InputIterator, class OutIterator>
OutputIterator adjacent_difference(
    InputIterator first,
    InputIterator last,
    OutputIterator result);

template <class InputIterator, class OutIterator, 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,
    ForwardIterator1 first,
    ForwardIterator1 last,
    ForwardIterator2 result);

template <class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
class BinaryOperation>
ForwardIterator2 adjacent_difference(
    ExecutionPolicy&& exec,
    ForwardIterator1 first,
    ForwardIterator1 last,
    ForwardIterator2 result,
    BinaryOperation binary_op);

参数

exec
执行策略。

first
输入迭代器,此迭代器在其中元素与各自前一元素不同的输入范围内或从中通过其他指定二元运算来操作值对的输入范围内发现第一个元素。

last
输入迭代器,此迭代器在其中元素与各自前一元素不同的输入范围内或从中通过其他指定二元运算来操作值对的输入范围内发现最后一个元素。

result
输出迭代器,此迭代器在存储一系列差值或指定运算结果的目标范围内发现第一个元素。

binary_op
在用于替换差分程序中减法运算的通用运算中应用的二元运算。

返回值

发现目标范围末尾的输出迭代器:result + (last - first)。

备注

输出迭代器 result 可以与输入迭代器 first 相同,这样便可以就地计算 adjacent_difference

对于输入范围中的值序列 a1, a2, a3,第一个模板函数将在目标范围中存储连续的 adjacent_difference 值:a1, a2 - a1, a3 - a2

对于输入范围中的值序列 a1, a2, a3,第二个模板函数将在目标范围中存储连续的 adjacent_difference 值:a1, a2 binary_opa1, a3 binary_opa2

二元运算 binary_op 无需具有关联性或可交换性,因为应用的运算顺序是指定的

示例

// numeric_adj_diff.cpp
// compile with: /EHsc
#include <vector>
#include <list>
#include <numeric>
#include <functional>
#include <iostream>

int main( )
{
   using namespace std;

   vector<int> V1( 10 ), V2( 10 );
   vector<int>::iterator VIter1, VIter2, VIterend, VIterend2;

   list <int> L1;
   list <int>::iterator LIter1, LIterend, LIterend2;

   int t;
   for ( t = 1 ; t <= 10 ; t++ )
   {
      L1.push_back( t * t );
   }

   cout << "The input list L1 is:\n ( " ;
   for ( LIter1 = L1.begin( ) ; LIter1 != L1.end( ) ; LIter1++ )
      cout << *LIter1 << " ";
   cout << ")." << endl;

   // The first member function for the adjacent_differences of
   // elements in a list output to a vector
   VIterend = adjacent_difference ( L1.begin ( ) , L1.end ( ) ,
      V1.begin ( ) );

   cout << "Output vector containing adjacent_differences is:\n ( " ;
   for ( VIter1 = V1.begin( ) ; VIter1 != VIterend ; VIter1++ )
      cout << *VIter1 << " ";
   cout << ")." << endl;

   // The second member function used to compute
   // the adjacent products of the elements in a list
   VIterend2 = adjacent_difference ( L1.begin ( ) , L1.end ( ) , V2.begin ( ) ,
      multiplies<int>( ) );

   cout << "The output vector with the adjacent products is:\n ( " ;
   for ( VIter2 = V2.begin( ) ; VIter2 != VIterend2 ; VIter2++ )
      cout << *VIter2 << " ";
   cout << ")." << endl;

   // Computation of adjacent_differences in place
   LIterend2 = adjacent_difference ( L1.begin ( ) , L1.end ( ) , L1.begin ( ) );
   cout << "In place output adjacent_differences in list L1 is:\n ( " ;
   for ( LIter1 = L1.begin( ) ; LIter1 != LIterend2 ; LIter1++ )
      cout << *LIter1 << " ";
   cout << ")." << endl;
}

exclusive_scan

给定初始值,通过使用 std::plus<>() 或某一范围内指定的二元运算符计算独占前缀求和运算。 将结果写入从指定目标开始的范围。 独占前缀求和表示第 n 个输入元素不包含在 n 个元素的总和中。 包含执行策略参数的重载根据指定的策略执行。

template<class InputIterator, class OutputIterator, class Type>
OutputIterator exclusive_scan(
    InputIterator first,
    InputIterator last,
    OutputIterator result,
    Type init);

template<class InputIterator, class OutputIterator, class Type, class BinaryOperation>
OutputIterator exclusive_scan(
    InputIterator first,
    InputIterator last,
    OutputIterator result,
    Type init,
    BinaryOperation binary_op);

template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class Type>
ForwardIterator2 exclusive_scan(
    ExecutionPolicy&& exec,
    ForwardIterator1 first,
    ForwardIterator1 last,
    ForwardIterator2 result,
    Type init);

template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class Type, class BinaryOperation>
ForwardIterator2 exclusive_scan(
    ExecutionPolicy&& exec,
    ForwardIterator1 first,
    ForwardIterator1 last,
    ForwardIterator2 result,
    Type init,
    BinaryOperation binary_op);

参数

exec
执行策略。

first
输入迭代器,使用 binary_op 在要求和或合并的范围内寻址第一个元素

last
输入迭代器,使用 binary_op 在要求和或合并的范围内寻址最后一个元素,即迭代累计中实际包含的最后一个元素之外的一个位置

result
输出迭代器,此迭代器在存储一系列和或指定运算结果的目标范围内发现第一个元素。

init
使用 binary_op 向其依次添加或合并每个元素的初始值

binary_op
要应用于指定范围内每个元素的二元运算及其上一应用的结果。

返回值

发现目标范围末尾的输出迭代器:result + (last - first)。

gcd

计算整数 m 和 n 的最大公约数。

template <class M, class N>
constexpr common_type_t<M,N> gcd(M m, N n);

参数

m、n
整型类型的值。

返回值

返回 m 和 n 的绝对值的最大公约数,如果 m 和 n 都为零,则返回零。 如果 m 或 n 的绝对值不能表示为 common_type_t<M,N> 类型的值,则结果将不确定

inclusive_scan

给定初始值,通过在某一范围内使用 std::plus<>() 或指定的二元运算符计算非独占前缀求和运算。 将结果写入从指定目标开始的范围。 非独占前缀求和表示第 n 个输入元素包含在 n 个元素的总和中。 包含执行策略参数的重载根据指定的策略执行。

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 Type>
OutputIterator inclusive_scan(
    InputIterator first,
    InputIterator last,
    OutputIterator result,
    BinaryOperation binary_op,
    Type init);

template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2>
ForwardIterator2 inclusive_scan(
    ExecutionPolicy&& exec,
    ForwardIterator1 first,
    ForwardIterator1 last,
    ForwardIterator2 result);

template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
class BinaryOperation>
ForwardIterator2 inclusive_scan(
    ExecutionPolicy&& exec,
    ForwardIterator1 first,
    ForwardIterator1 last,
    ForwardIterator2 result,
    BinaryOperation binary_op);

template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
class BinaryOperation, class Type>
ForwardIterator2 inclusive_scan(
    ExecutionPolicy&& exec,
    ForwardIterator1 first,
    ForwardIterator1 last,
    ForwardIterator2 result,
    BinaryOperation binary_op,
    Type init);

参数

exec
执行策略。

first
输入迭代器,使用 binary_op 在要求和或合并的范围内寻址第一个元素

last
输入迭代器,使用 binary_op 在要求和或合并的范围内寻址最后一个元素,即迭代累计中实际包含的最后一个元素之外的一个位置

result
输出迭代器,此迭代器在存储一系列和或指定运算结果的目标范围内发现第一个元素。

init
使用 binary_op 向其依次添加或合并每个元素的初始值

binary_op
要应用于指定范围内每个元素的二元运算及其上一应用的结果。

返回值

发现目标范围末尾的输出迭代器:result + (last - first)。

inner_product

计算两个范围的逐元素集乘积的总和并将总和添加到指定初始值,或计算将求和与乘积二元运算替换为其他指定二元运算的一般化程序的结果。

template <class InputIterator1, class InputIterator2, class Type>
Type inner_product(
    InputIterator1   first1,
    InputIterator1   last1,
    InputIterator2   first2,
    Type             init);

template <class InputIterator1, class InputIterator2, class Type, class BinaryOperation1, class BinaryOperation2>
Type inner_product(
    InputIterator1   first1,
    InputIterator1   last1,
    InputIterator2   first2,
    Type             init,
    BinaryOperation1 binary_op1,
    BinaryOperation2 binary_op2);

参数

first1
一个输入迭代器,该迭代器发现第一个范围内的第一个元素,该范围与第二个范围的内部乘积或一般化内部乘积将进行计算。

last1
一个输入迭代器,该迭代器发现第一个范围内的最后一个元素,该范围与第二个范围的内部乘积或一般化内部乘积将进行计算。

first2
一个输入迭代器,该迭代器发现第二个范围内的第一个元素,该范围与第一个范围的内部乘积或一般化内部乘积将进行计算。

init
一个初始值,将对该值添加两个范围间的内部乘积或一般化内部乘积。

binary_op1
在一般化内部乘积中,替换应用于逐元素乘积的内部乘积求和运算的二元运算。

binary_op2
在一般化内部乘积中,替换内部乘积逐元素乘积运算的二元运算。

返回值

第一个成员函数返回逐元素集乘积的总和,并向其添加指定的初始值。 因此,对于值 ai 和 bi 的范围,它将返回:

init + (a1 * b1) + (a2 * b2) + ... + (an * bn)

方法是以迭代方式将 init 替换为 init + (ai * bi)

第二个成员函数返回:

initbinary_op1 (a1 binary_op2b1) binary_op1 (a2 binary_op2b2) binary_op1 ... binary_op1 (an binary_op2bn)

方法是以迭代方式将 init 替换为 initbinary_op1 (ai binary_op2bi)

备注

初始值确保当范围为空时具有明确定义的结果。 在这种情况下,将返回 init。 二元运算不需要具有关联性或可交换性。 该范围必须有效,并且其复杂度与该范围的大小呈线性关系。 二元运算符的返回类型必须可转换为 Type,以确保在迭代期间闭包。

示例

// numeric_inner_prod.cpp
// compile with: /EHsc
#include <vector>
#include <list>
#include <numeric>
#include <functional>
#include <iostream>

int main()
{
   using namespace std;

   vector <int> v1, v2(7), v3(7);
   vector <int>::iterator iter1, iter2, iter3;

   int i;
   for (i = 1; i <= 7; i++)
   {
      v1.push_back(i);
   }

   cout << "The original vector v1 is:\n ( " ;
   for (iter1 = v1.begin(); iter1 != v1.end(); iter1++)
      cout << *iter1 << " ";
   cout << ")." << endl;

   list <int> l1, l2(7);
   list <int>::iterator lIter1, lIter2;

   int t;
   for (t = 1; t <= 7; t++)
   {
      l1.push_back(t);
   }

   cout << "The original list l1 is:\n ( " ;
   for (lIter1 = l1.begin(); lIter1 != l1.end(); lIter1++)
      cout << *lIter1 << " ";
   cout << ")." << endl;

   // The first member function for the inner product
   int inprod;
   inprod = inner_product(v1.begin(), v1.end(), l1.begin(), 0);

   cout << "The inner_product of the vector v1 and the list l1 is: "
        << inprod << "." << endl;

   // Constructing a vector of partial inner_products between v1 & l1
   int j = 0, parinprod;
   for (iter1 = v1.begin(); iter1 != v1.end(); iter1++) {
      parinprod = inner_product(v1.begin(), iter1 + 1, l1.begin(), 0);
      v2[j] = parinprod;
      j++;
   }

   cout << "Vector of partial inner_products between v1 & l1 is:\n ( " ;
   for (iter2 = v2.begin(); iter2 != v2.end(); iter2++)
      cout << *iter2 << " ";
   cout << ")." << endl << endl;

   // The second member function used to compute
   // the product of the element-wise sums
   int inprod2;
   inprod2 = inner_product (v1.begin(), v1.end(),
      l1.begin(), 1, multiplies<int>(), plus<int>());

   cout << "The sum of the element-wise products of v1 and l1 is: "
        << inprod2 << "." << endl;

   // Constructing a vector of partial sums of element-wise products
   int k = 0, parinprod2;
   for (iter1 = v1.begin(); iter1 != v1.end(); iter1++)
   {
      parinprod2 =
         inner_product(v1.begin(), iter1 + 1, l1.begin(), 1,
         multiplies<int>(), plus<int>());
      v3[k] = parinprod2;
      k++;
   }

   cout << "Vector of partial sums of element-wise products is:\n ( " ;
   for (iter3 = v3.begin(); iter3 != v3.end(); iter3++)
      cout << *iter3 << " ";
   cout << ")." << endl << endl;
}

iota

存储一个起始值,从第一个元素开始,在间隔 [first, last) 内的每个元素中填充此值的连续递增值 (value++)。

template <class ForwardIterator, class Type>
void iota(ForwardIterator first, ForwardIterator last, Type value);

参数

first
发现范围中要填充的第一个元素的输入迭代器。

last
发现范围中要填充的最后一个元素的输入迭代器。

value
要存储在第一个元素中并连续增加后续元素的起始值。

示例

以下示例通过填充整数列表,然后通过 list 填充向量演示了 iota 函数的一些用途,以便使用 random_shuffle 函数。

// compile by using: cl /EHsc /nologo /W4 /MTd
#include <algorithm>
#include <numeric>
#include <list>
#include <vector>
#include <iostream>

using namespace std;

int main(void)
{
    list <int> intList(10);
    vector <list<int>::iterator> intVec(intList.size());

    // Fill the list
    iota(intList.begin(), intList.end(), 0);

    // Fill the vector with the list so we can shuffle it
    iota(intVec.begin(), intVec.end(), intList.begin());

    random_shuffle(intVec.begin(), intVec.end());

    // Output results
    cout << "Contents of the integer list: " << endl;
    for (auto i: intList) {
        cout << i << ' ';
    }
    cout << endl << endl;

    cout << "Contents of the integer list, shuffled by using a vector: " << endl;
    for (auto i: intVec) {
        cout << *i << ' ';
    }
    cout << endl;
}

lcm

template <class M, class N>
constexpr common_type_t<M,N> lcm(M m, N n);

partial_sum

计算输入范围中从第一个元素到第 n 个元素的一系列总和,并在目标范围的第 n 个元素中存储每个总和的结果。 或者,计算将求和运算替换为其他指定二元运算的一般化程序的结果。

template <class InputIterator, class OutIt>
OutputIterator partial_sum(
    InputIterator first,
    InputIterator last,
    OutputIterator result);

template <class InputIterator, class OutIt, class Fn2>
OutputIterator partial_sum(
    InputIterator first,
    InputIterator last,
    OutputIterator result,
    BinaryOperation binary_op);

参数

first
输入迭代器,此迭代器在要部分求和或根据指定二元运算合并的范围内发现第一个元素。

last
输入迭代器,此迭代器在要部分求和或根据指定二元运算合并的范围内发现最后一个元素,即迭代累计中实际包含的最后一个元素之外的一个位置。

result
输出迭代器,此迭代器在要存储一系列部分和或指定二元运算的连续结果的目标范围内寻址第一个元素。

binary_op
在用于替换部分求和程序中求和运算的通用运算中应用的二元运算。

返回值

发现目标范围末尾的输出迭代器:result + (last - first)。

备注

输出迭代器 result 可以与输入迭代器 first 相同,这样便可以就地计算部分和

对于输入范围中的值序列 a1, a2, ... ax,第一个模板函数将在目标范围中存储连续的部分和。 第 n 个元素由 (a1 + a2 + a3 + ... + an) 提供

对于输入范围中的值序列 a1, a2, a3,第二个模板函数将在目标范围中存储连续的部分结果。 第 n 个元素由 ((...((a1 binary_opa2) binary_opa3) binary_op ... ) binary_opan) 提供

二元运算 binary_op 无需具有关联性或可交换性,因为应用的运算顺序是指定的

示例

// numeric_partial_sum.cpp
// compile with: /EHsc
#include <vector>
#include <list>
#include <numeric>
#include <functional>
#include <iostream>

int main( )
{
   using namespace std;
   vector<int> V1( 10 ), V2( 10 );
   vector<int>::iterator VIter1, VIter2, VIterend, VIterend2;

   list <int> L1;
   list <int>::iterator LIter1, LIterend;

   int t;
   for ( t = 1 ; t <= 10 ; t++ )
   {
      L1.push_back( t );
   }

   cout << "The input list L1 is:\n ( " ;
   for ( LIter1 = L1.begin( ) ; LIter1 != L1.end( ) ; LIter1++ )
      cout << *LIter1 << " ";
   cout << ")." << endl;

   // The first member function for the partial sums of
   // elements in a list output to a vector
   VIterend = partial_sum ( L1.begin ( ) , L1.end ( ) ,
      V1.begin ( ) );

   cout << "The output vector containing the partial sums is:\n ( " ;
   for ( VIter1 = V1.begin( ) ; VIter1 != VIterend ; VIter1++ )
      cout << *VIter1 << " ";
   cout << ")." << endl;

   // The second member function used to compute
   // the partial product of the elements in a list
   VIterend2 = partial_sum ( L1.begin ( ) , L1.end ( ) , V2.begin ( ) ,
      multiplies<int>( ) );

   cout << "The output vector with the partial products is:\n ( " ;
   for ( VIter2 = V2.begin( ) ; VIter2 != VIterend2 ; VIter2++ )
      cout << *VIter2 << " ";
   cout << ")." << endl;

   // Computation of partial sums in place
   LIterend = partial_sum ( L1.begin ( ) , L1.end ( ) , L1.begin ( ) );
   cout << "The in place output partial_sum list L1 is:\n ( " ;
   for ( LIter1 = L1.begin( ) ; LIter1 != LIterend ; LIter1++ )
      cout << *LIter1 << " ";
   cout << ")." << endl;
}

reduce

通过以任意且可能置换的顺序计算总和,减少指定范围内的所有元素(可能包括一些初始值)。 或者,通过计算指定二元运算的结果来减少。 包含执行策略参数的重载根据指定的策略执行。

template<class InputIterator>
typename iterator_traits<InputIterator>::value_type reduce(
    InputIterator first,
    InputIterator last);

template<class InputIterator, class Type>
Type reduce(
    InputIterator first,
    InputIterator last,
    Type init);

template<class InputIterator, class Type, class BinaryOperation>
Type reduce(
    InputIterator first,
    InputIterator last,
    Type init,
    BinaryOperation binary_op);

template<class ExecutionPolicy, class ForwardIterator>
typename iterator_traits<ForwardIterator>::value_type reduce(
    ExecutionPolicy&& exec,
    ForwardIterator first,
    ForwardIterator last);

template<class ExecutionPolicy, class ForwardIterator, class Type>
Type reduce(
    ExecutionPolicy&& exec,
    ForwardIterator first,
    ForwardIterator last,
    Type init);

template<class ExecutionPolicy, class ForwardIterator, class Type, class BinaryOperation>
Type reduce(
    ExecutionPolicy&& exec,
    ForwardIterator first,
    ForwardIterator last,
    Type init,
    BinaryOperation binary_op);

参数

exec
执行策略。

first
输入迭代器,使用 binary_op 在要求和或合并的范围内寻址第一个元素

last
输入迭代器,使用 binary_op 在要求和或合并的范围内寻址最后一个元素,即迭代累计中实际包含的最后一个元素之外的一个位置

result
输出迭代器,此迭代器在存储一系列和或指定运算结果的目标范围内发现第一个元素。

init
使用 binary_op 向其依次添加或合并每个元素的初始值

binary_op
要应用于指定范围内每个元素的二元运算及其上一应用的结果。

返回值

将 binary_op 或 std::plus<>() 应用于 init 并将指定范围内的所有元素应用于 (*PartialResult, in_iter) 的结果,其中 PartialResult 是该运算的上一应用的结果,in_iter 是指向范围内某个元素的迭代器。 在未指定 init 的重载中,使用的 init 值等同于 typename iterator_traits<InputIterator>::value_type{}

注解

reduce 行为是不确定的,除非 binary_op 具有关联性和可交换性。 如果 binary_op 修改任何元素,或使区间 [first, last](含)内的任何迭代器无效,则行为将不确定

transform_exclusive_scan

使用指定的一元运算符转换范围的元素,然后在给定初始值的情况下,通过使用 std::plus<>() 或范围内指定的二元运算符计算独占前缀求和运算。 将结果写入从指定目标开始的范围。 独占前缀求和表示第 n 个输入元素不包含在 n 个元素的总和中。 包含执行策略参数的重载根据指定的策略执行。 可以以任意顺序执行求和。

template<class InputIterator, class OutputIterator, class Type, class BinaryOperation, class UnaryOperation>
OutputIterator transform_exclusive_scan(
    InputIterator first,
    InputIterator last,
    OutputIterator result,
    Type init,
    BinaryOperation binary_op,
    UnaryOperation unary_op);

template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class Type, class BinaryOperation, class UnaryOperation>
ForwardIterator2 transform_exclusive_scan(
    ExecutionPolicy&& exec,
    ForwardIterator1 first,
    ForwardIterator1 last,
    ForwardIterator2 result,
    Type init,
    BinaryOperation binary_op,
    UnaryOperation unary_op);

参数

exec
执行策略。

first
输入迭代器,使用 binary_op 在要求和或合并的范围内寻址第一个元素

last
输入迭代器,使用 binary_op 在要求和或合并的范围内寻址最后一个元素,即迭代累计中实际包含的最后一个元素之外的一个位置

result
输出迭代器,此迭代器在存储一系列和或指定运算结果的目标范围内发现第一个元素。

init
使用 binary_op 向其依次添加或合并每个元素的初始值

binary_op
要应用于指定范围内每个元素的二元运算及其上一应用的结果。

unary_op
要应用于指定范围内的每个元素的一元运算。

transform_inclusive_scan

使用指定的一元运算符转换范围的元素,然后在给定初始值的情况下,通过使用 std::plus<>() 或范围内指定的二元运算符计算非独占前缀求和运算。 将结果写入从指定目标开始的范围。 非独占前缀求和表示第 n 个输入元素包含在 n 个元素的总和中。 包含执行策略参数的重载根据指定的策略执行。 可以以任意顺序执行求和。

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 Type>
OutputIterator transform_inclusive_scan(
    InputIterator first,
    InputIterator last,
    OutputIterator result,
    BinaryOperation binary_op,
    UnaryOperation unary_op,
    Type init);

template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class BinaryOperation, class UnaryOperation>
ForwardIterator2 transform_inclusive_scan(
    ExecutionPolicy&& exec,
    ForwardIterator1 first,
    ForwardIterator1 last,
    ForwardIterator2 result,
    BinaryOperation binary_op,
    UnaryOperation unary_op);

template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class BinaryOperation, class UnaryOperation, class Type>
ForwardIterator2 transform_inclusive_scan(
    ExecutionPolicy&& exec,
    ForwardIterator1 first,
    ForwardIterator1 last,
    ForwardIterator2 result,
    BinaryOperation binary_op,
    UnaryOperation unary_op,
    Type init);

参数

exec
执行策略。

first
输入迭代器,使用 binary_op 在要求和或合并的范围内寻址第一个元素

last
输入迭代器,使用 binary_op 在要求和或合并的范围内寻址最后一个元素,即迭代累计中实际包含的最后一个元素之外的一个位置

result
输出迭代器,此迭代器在存储一系列和或指定运算结果的目标范围内发现第一个元素。

binary_op
要应用于指定范围内每个元素的二元运算及其上一应用的结果。

unary_op
要应用于指定范围内的每个元素的一元运算。

init
使用 binary_op 向其依次添加或合并每个元素的初始值

transform_reduce

转换一系列元素,然后应用一个函子以任意顺序减少转换后的元素。 实际上,transform 后跟 reduce

template<class InputIterator1, class InputIterator2, class Type>
Type transform_reduce(
    InputIterator1 first1,
    InputIterator1 last1,
    InputIterator2 first2,
    Type init);

template<class InputIterator1, class InputIterator2, class Type, class BinaryOperation1, class BinaryOperation2>
Type transform_reduce(
    InputIterator1 first1,
    InputIterator1 last1,
    InputIterator2 first2,
    Type init,
    BinaryOperation1 binary_op1,
    BinaryOperation2 binary_op2);

template<class InputIterator, class Type, class BinaryOperation, class UnaryOperation>
Type transform_reduce(
    InputIterator first,
    InputIterator last,
    Type init,
    BinaryOperation binary_op,
    UnaryOperation unary_op);

template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class Type>
Type transform_reduce(
    ExecutionPolicy&& exec,
    ForwardIterator1 first1,
    ForwardIterator1 last1,
    ForwardIterator2 first2,
    Type init);

template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class Type, class BinaryOperation1, class BinaryOperation2>
Type transform_reduce(
    ExecutionPolicy&& exec,
    ForwardIterator1 first1,
    ForwardIterator1 last1,
    ForwardIterator2 first2,
    Type init,
    BinaryOperation1 binary_op1,
    BinaryOperation2 binary_op2);

template<class ExecutionPolicy, class ForwardIterator, class Type, class BinaryOperation, class UnaryOperation>
Type transform_reduce(
    ExecutionPolicy&& exec,
    ForwardIterator first,
    ForwardIterator last,
    Type init,
    BinaryOperation binary_op,
    UnaryOperation unary_op);

参数

exec
执行策略。

first
输入迭代器,使用 binary_op 在要求和或合并的范围内寻址第一个元素

first1
输入迭代器,使用 binary_op1 在要求和或合并的范围内寻址第一个元素

last
输入迭代器,使用 binary_op 在要求和或合并的范围内寻址最后一个元素,即迭代累计中实际包含的最后一个元素之外的一个位置

last1
输入迭代器,使用 binary_op1 在要求和或合并的范围内寻址最后一个元素,即迭代累计中实际包含的最后一个元素之外的一个位置

result
输出迭代器,此迭代器在存储一系列和或指定运算结果的目标范围内发现第一个元素。

init
使用 binary_op 向其依次添加或合并每个元素的初始值

binary_op
要应用于指定范围内每个元素的二元运算及其上一应用的结果。

unary_op
要应用于指定范围内的每个元素的一元运算。

返回值

转换然后减少的结果。