std::ranges:: for_each_n, std::ranges:: for_each_n_result
           std::ranges
          
          | Non-modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 
 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 
 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Partitioning operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sorting operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Binary search operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Set operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Heap operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Minimum/maximum operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Permutation operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Fold operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 
 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 
 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operations on uninitialized storage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Return types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 
           定义于头文件
            
            
             <algorithm>
            
            | ||
| 
           调用签名
           | ||
| 
           
            
             template
            
            
             <
            
            
             
              std::
              
               input_iterator
              
             
            
            I,
            
             class
            
            Proj
            
             =
            
            
             
              std::
              
               identity
              
             
            
            ,
             
             
              
               std::
               
                indirectly_unary_invocable
               
              
             
             
              <
             
             std
             
              ::
             
             
              
               projected
              
             
             
              <
             
             I, Proj
             
              >>
             
             Fun
             
              >
             
              | (1) | (C++20 起) | 
| 
           辅助类型
           | ||
| 
           
            
             template
            
            
             <
            
            
             class
            
            I,
            
             class
            
            F
            
             >
            
             using for_each_n_result = ranges:: in_fun_result < I, F > ; | (2) | (C++20 起) | 
         [
        
        
         
          first
         
        
        
         ,
        
        
         
          first
          
           +
          
          n
         
        
        
         )
        
       
       中的每个迭代器,按顺序执行。
      如果迭代器类型是可变的, f 可以通过解引用迭代器修改范围内的元素。若 f 返回结果,该结果将被忽略。如果 n 小于零,则行为未定义。
本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:
| 目录 | 
参数
| first | - | 指向待处理范围起始位置的迭代器 | 
| n | - | 待处理的元素数量 | 
| f | - | 应用于投影范围的函数 
           [
          first
           ,
          first
            
             +
            
            n
           )
           | 
| proj | - | 应用于元素的投影 | 
返回值
一个对象 { first + n, std :: move ( f ) } ,其中 first + n 可根据迭代器类别被求值为 std :: ranges:: next ( std :: move ( first ) , n ) 。
复杂度
恰好应用 n 次 f 和 proj 。
可能的实现
struct for_each_n_fn { template<std::input_iterator I, class Proj = std::identity, std::indirectly_unary_invocable<std::projected<I, Proj>> Fun> constexpr for_each_n_result<I, Fun> operator()(I first, std::iter_difference_t<I> n, Fun fun, Proj proj = Proj{}) const { for (; n-- > 0; ++first) std::invoke(fun, std::invoke(proj, *first)); return {std::move(first), std::move(fun)}; } }; inline constexpr for_each_n_fn for_each_n {};
示例
#include <algorithm> #include <array> #include <iostream> #include <ranges> #include <string_view> struct P { int first; char second; friend std::ostream& operator<<(std::ostream& os, const P& p) { return os << '{' << p.first << ",'" << p.second << "'}"; } }; auto print = [](std::string_view name, auto const& v) { std::cout << name << ": "; for (auto n = v.size(); const auto& e : v) std::cout << e << (--n ? ", " : "\n"); }; int main() { std::array a {1, 2, 3, 4, 5}; print("a", a); // 对前三个数字取反: std::ranges::for_each_n(a.begin(), 3, [](auto& n) { n *= -1; }); print("a", a); std::array s { P{1,'a'}, P{2, 'b'}, P{3, 'c'}, P{4, 'd'} }; print("s", s); // 使用投影对数据成员 'P::first' 取反: std::ranges::for_each_n(s.begin(), 2, [](auto& x) { x *= -1; }, &P::first); print("s", s); // 使用投影将数据成员 'P::second' 大写化: std::ranges::for_each_n(s.begin(), 3, [](auto& c) { c -= 'a'-'A'; }, &P::second); print("s", s); }
输出:
a: 1, 2, 3, 4, 5
a: -1, -2, -3, 4, 5
s: {1,'a'}, {2,'b'}, {3,'c'}, {4,'d'}
s: {-1,'a'}, {-2,'b'}, {3,'c'}, {4,'d'}
s: {-1,'A'}, {-2,'B'}, {3,'C'}, {4,'d'}
        参见
| 范围 
           for
          循环
         
         
          (C++11) | 在范围上执行循环 | 
| 
           
            
             
              (C++20)
             
            
           
           | 对
         
          范围
         
         中的元素应用一元
         
          函数对象 (算法函数对象) | 
| 
           
            
             
              (C++17)
             
            
           
           | 对序列的前N个元素应用函数对象 (函数模板) | 
| 对
         
          范围
         
         中的元素应用一元
         
          函数对象 (函数模板) |