std:: transform_exclusive_scan
| 
           定义于头文件
            
            
             <numeric>
            
            | ||
| 
           
            
             template
            
            
             <
            
            
             class
            
            InputIt,
            
             class
            
            OutputIt,
            
             class
            
            T,
             
             
              class
             
             BinaryOp,
             
              class
             
             UnaryOp
             
              >
             
              | (1) | (C++17 起) (C++20 起为 constexpr) | 
| 
           
            
             template
            
            
             <
            
            
             class
            
            ExecutionPolicy,
             
             
              class
             
             ForwardIt1,
             
              class
             
             ForwardIt2,
             
              class
             
             T,
              | (2) | (C++17 起) | 
         [
        
        
         
          
          
           0
          
          
         
        
        
         ,
        
        
         
          
           
            std::
            
             distance
            
           
          
          
           (
          
          first, last
          
           )
          
         
        
        
         )
        
       
       内的每个整数
       
        
         i
        
       
       ,按顺序执行以下操作:
       - 
         创建一个序列,该序列由
         
          
           init
          
         
         开头,后接通过
         
          
           unary_op
          
         
         按顺序转换区间
         
          [first,iter)元素得到的值,其中 iter 是 first 的第 i 个 迭代器。
- 通过 binary_op 计算该序列的广义非交换和。
- 将结果赋值给 * dest ,其中 dest 是 d_first 的第 i 个 迭代器。
| std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> 为 true 。 | (C++20 前) | 
| std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> 为 true 。 | (C++20 起) | 
序列元素关于二元运算 binary_op 的 广义非交换和 定义如下:
- 如果序列只有一个元素,则总和为该元素的值。
- 否则,按顺序执行以下操作:
- 从序列中选择任意两个相邻元素 elem1 和 elem2 。
- 计算 binary_op ( elem1, elem2 ) 并将序列中的这两个元素替换为计算结果。
- 重复步骤1和2,直到序列中仅剩一个元素。
       
       如果
       
        
         binary_op
        
       
       不具有结合性(例如浮点数加法),则结果将是非确定性的。
      
       如果以下任意值无法转换为
       
        T
       
       ,则程序非良构:
      
- binary_op ( init, init )
- binary_op ( init, unary_op ( * first ) )
- binary_op ( unary_op ( * first ) , unary_op ( * first ) )
       标签内的C++代码均未翻译,仅翻译了需要本地化的说明文本。由于原文中除代码外无其他可翻译文本,故保持原样输出)
      
      若满足以下任一条件,则行为未定义:
- 
        T不满足 MoveConstructible 要求。
- 
        
         
          unary_op
         
        
        或
        
         
          binary_op
         
        
        修改了
        
         [first,last)范围内的任何元素。
- 
        
         
          unary_op
         
        
        或
        
         
          binary_op
         
        
        使
        
         [first,last]范围内的任何迭代器或子区间失效。
| 目录 | 
参数
| first, last | - | 定义待求和元素 范围 的迭代器对 | 
| d_first | - | 目标范围的起始位置,可等于 first | 
| policy | - | 使用的 执行策略 | 
| init | - | 初始值 | 
| unary_op | - | 将应用于输入范围每个元素的一元 函数对象 。其返回类型必须可作为 binary_op 的输入 | 
| binary_op | - | 将应用于 unary_op 结果、其他 binary_op 结果及 init 的二元 函数对象 | 
| 类型要求 | ||
| - 
          InputIt
         必须满足
         
          
           LegacyInputIterator
          
         
         要求 | ||
| - 
          OutputIt
         必须满足
         
          
           LegacyOutputIterator
          
         
         要求 | ||
| - 
          ForwardIt1, ForwardIt2
         必须满足
         
          
           LegacyForwardIterator
          
         
         要求 | ||
返回值
指向已写入的最后一个元素之后位置的迭代器。
复杂度
给定 N 为 std:: distance ( first, last ) :
异常
       带有名为
       
        ExecutionPolicy
       
       模板参数的重载按以下方式报告错误:
      
- 
        如果作为算法一部分调用的函数执行抛出异常,且
        ExecutionPolicy是某个 标准策略 ,则调用 std::terminate 。对于任何其他ExecutionPolicy,其行为由实现定义。
- 如果算法无法分配内存,则抛出 std::bad_alloc 。
注释
unary_op 永远不会应用于 init 。
示例
#include <functional> #include <iostream> #include <iterator> #include <numeric> #include <vector> int main() { std::vector data{3, 1, 4, 1, 5, 9, 2, 6}; auto times_10 = [](int x) { return x * 10; }; std::cout << "10倍排他和: "; std::transform_exclusive_scan(data.begin(), data.end(), std::ostream_iterator<int>(std::cout, " "), 0, std::plus<int>{}, times_10); std::cout << "\n10倍包含和: "; std::transform_inclusive_scan(data.begin(), data.end(), std::ostream_iterator<int>(std::cout, " "), std::plus<int>{}, times_10); std::cout << '\n'; }
输出:
10倍排他和: 0 30 40 80 90 140 230 250 10倍包含和: 30 40 80 90 140 230 250 310
参见
| 计算范围内元素的部分和 (函数模板) | |
| 
           
            
             
              (C++17)
             
            
           
           | 类似于
         
          
           std::partial_sum
          
         
         ,但从第
         
          i
         
         个和中排除第
         
          i
         
         个输入元素 (函数模板) | 
| 
           
            
             
              (C++17)
             
            
           
           | 应用可调用对象后计算包含性扫描 (函数模板) |