std:: for_each_n
| 
           定义于头文件
            
            
             <algorithm>
            
            | ||
| 
           
            
             template
            
            
             <
            
            
             class
            
            InputIt,
            
             class
            
            Size,
            
             class
            
            UnaryFunc
            
             >
            
             InputIt for_each_n ( InputIt first, Size n, UnaryFunc f ) ; | (1) | (C++17 起) (C++20 起为 constexpr) | 
| 
           
            
             template
            
            
             <
            
            
             class
            
            ExecutionPolicy,
             
             
              class
             
             ForwardIt,
             
              class
             
             Size,
             
              class
             
             UnaryFunc
             
              >
             
              | (2) | (C++17 起) | 
       对范围
       
        
         [
        
        
         
          first
         
        
        
         ,
        
        
         
          first
          
           +
          
          n
         
        
        
         )
        
       
       内每个迭代器解引用后的结果应用给定的函数对象
       
        
         f
        
       
       。若
       
        
         f
        
       
       返回结果,该结果将被忽略。
      
| 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 起) | 
如果 n >= 0 不为 true ,则行为是未定义的。
       如果迭代器类型(
       
        InputIt
       
       /
       
        ForwardIt
       
       )是可变的,
       
        
         f
        
       
       可能通过解引用迭代器来修改范围内的元素。
      
       与其他并行算法不同,
       
        for_each_n
       
       不允许复制序列中的元素,即使它们是
       
        
         TriviallyCopyable
        
       
       的。
      
| 目录 | 
参数
| first | - | 要应用函数的范围起始位置 | 
| n | - | 要应用函数的元素数量 | 
| policy | - | 使用的 执行策略 | 
| f | - | 函数对象,将应用于范围 
           [
          first
           ,
          first
            
             +
            
            n
           )
          内每个迭代器解引用结果函数签名应等价于: void fun ( const Type & a ) ; 
          签名无需包含
          
           
            
             const
            
            
             &
            
           
          
          。
            | 
| 类型要求 | ||
| - 
          InputIt
         必须满足
         
          
           LegacyInputIterator
          
         
         的要求。 | ||
| - 
          ForwardIt
         必须满足
         
          
           LegacyForwardIterator
          
         
         的要求。 | ||
| - 
          Size
         必须可转换为整数类型。 | ||
返回值
一个等于 first + n 的迭代器,或更正式地说,等于 std:: advance ( first, n ) 的迭代器。
复杂度
恰好应用 f 函数 n 次。
异常
       带有名为
       
        ExecutionPolicy
       
       模板参数的重载按以下方式报告错误:
      
- 
        如果作为算法一部分调用的函数执行抛出异常,且
        ExecutionPolicy是某个 标准策略 ,则调用 std::terminate 。对于其他任何ExecutionPolicy,其行为由实现定义。
- 如果算法无法分配内存,则抛出 std::bad_alloc 。
可能的实现
另请参阅 libstdc++ 、 libc++ 与 MSVC stdlib 中的实现。
| template<class InputIt, class Size, class UnaryFunc> InputIt for_each_n(InputIt first, Size n, UnaryFunc f) { for (Size i = 0; i < n; ++first, (void) ++i) f(*first); return first; } | 
示例
#include <algorithm> #include <iostream> #include <vector> void println(auto const& v) { for (auto count{v.size()}; const auto& e : v) std::cout << e << (--count ? ", " : "\n"); } int main() { std::vector<int> vi{1, 2, 3, 4, 5}; println(vi); std::for_each_n(vi.begin(), 3, [](auto& n) { n *= 2; }); println(vi); }
输出:
1, 2, 3, 4, 5 2, 4, 6, 4, 5
参见
| 对范围内的元素应用函数,并将结果存储到目标范围 (函数模板) | |
| range- 
           for
          循环
         
         
          (C++11) | 在范围上执行循环 | 
| 对来自
         
          范围
         
         的元素应用一元
         
          函数对象 (函数模板) | |
| 
           
            
             
              (C++20)
             
            
           
           | 对序列的前N个元素应用函数对象 (算法函数对象) |