std::ranges::for_each, std::ranges::for_each_result

来自cppreference.com
< cpp‎ | algorithm‎ | ranges
 
 
算法库
受约束算法及范围上的算法(C++20)
包含算法例如 ranges::copy, ranges::sort, ...
执行策略 (C++17)
排序和相关操作
划分操作
排序操作
二分搜索操作(在已划分范围上)
集合操作(在有序范围上)
归并操作(在有序范围上)
堆操作
最小/最大操作
(C++11)
(C++17)
字典序比较操作
排列操作
C 库

数值运算
(C++11)                       
在未初始化内存上的操作
 
受约束算法
本菜单中的所有名字均属于命名空间 std::ranges
不修改序列的操作
修改序列的操作
划分操作
排序操作
二分搜索操作(在有序范围上)
       
       
集合操作(在有序范围上)
堆操作
最小/最大操作
排列操作
折叠操作
数值操作
(C++23)            
未初始化存储上的操作
返回类型
 
在标头 <algorithm> 定义
调用签名
template<std::input_iterator I, std::sentinel_for<I> S, class Proj =std::identity,

          std::indirectly_unary_invocable<std::projected<I, Proj>> Fun >
constexpr for_each_result<I, Fun>

    for_each( I first, S last, Fun f, Proj proj ={});
(1) (C++20 起)
template<ranges::input_range R, class Proj =std::identity,

          std::indirectly_unary_invocable<
              std::projected<ranges::iterator_t<R>, Proj>> Fun >
constexpr for_each_result<ranges::borrowed_iterator_t<R>, Fun>

    for_each( R&& r, Fun f, Proj proj ={});
(2) (C++20 起)
辅助类型
template<class I, class F >
using for_each_result =ranges::in_fun_result<I, F>;
(3) (C++20 起)
1) 按顺序,将给定的函数对象 f 应用于范围 [firstlast) 中每个迭代器投影的值的结果。
2)(1) 相同,但以 r 为源范围,如同以 ranges::begin(r)first 并以 ranges::end(r)last

对于两个重载,如果迭代器的类型是可变的,则 f 可能会通过解引用迭代器去修改范围中的元素。如果 f 会返回结果,则忽略此结果。

此页面上描述的函数式实体是算法函数对象(非正式地称为 niebloid),即:

目录

[编辑]参数

first, last - 要对之应用函数的元素范围的迭代器-哨位对
r - 要将函数应用到的元素范围
f - 应用到投影后范围的元素
proj - 应用范围的投影

[编辑]返回值

{ranges::next(std::move(first), last), std::move(f)}

[编辑]复杂度

准确 ranges::distance(first, last) 次应用 fproj

[编辑]可能的实现

struct for_each_fn {template<std::input_iterator I, std::sentinel_for<I> S, class Proj =std::identity, std::indirectly_unary_invocable<std::projected<I, Proj>> Fun>constexpr ranges::for_each_result<I, Fun> operator()(I first, S last, Fun f, Proj proj ={})const{for(; first != last;++first)std::invoke(f, std::invoke(proj, *first));return{std::move(first), std::move(f)};}   template<ranges::input_range R, class Proj =std::identity, std::indirectly_unary_invocable<std::projected<ranges::iterator_t<R>, Proj>> Fun>constexpr ranges::for_each_result<ranges::borrowed_iterator_t<R>, Fun> operator()(R&& r, Fun f, Proj proj ={})const{return(*this)(ranges::begin(r), ranges::end(r), std::move(f), std::ref(proj));}};   inlineconstexpr for_each_fn for_each;

[编辑]示例

下述的示例用 lambda 表达式递增 vector 所有的元素,然后用函数对象中重载的 operator() 计算它们的和。注意,要计算总和的话,建议使用专用的算法 std::accumulate

#include <algorithm>#include <cassert>#include <iostream>#include <string>#include <utility>#include <vector>   struct Sum {void operator()(int n){ sum += n;}int sum{0};};   int main(){std::vector<int> nums{3, 4, 2, 8, 15, 267};   auto print =[](constauto& n){std::cout<<' '<< n;};   namespace ranges = std::ranges;std::cout<<"之前:"; ranges::for_each(std::as_const(nums), print); print('\n');   ranges::for_each(nums, [](int& n){++n;});   // 对每个数调用 Sum::operator()auto[i, s]= ranges::for_each(nums.begin(), nums.end(), Sum());assert(i == nums.end());   std::cout<<"之后: "; ranges::for_each(nums.cbegin(), nums.cend(), print);   std::cout<<"\n""和: "<< s.sum<<'\n';   using pair =std::pair<int, std::string>;std::vector<pair> pairs{{1,"one"}, {2,"two"}, {3,"three"}};   std::cout<<"投影 pair::first: "; ranges::for_each(pairs, print, [](const pair& p){return p.first;});   std::cout<<"\n""投影 pair::second:"; ranges::for_each(pairs, print, &pair::second); print('\n');}

输出:

之前: 3 4 2 8 15 267 之后: 4 5 3 9 16 268 和: 305 投影 pair::first: 1 2 3 投影 pair::second: one two tree

[编辑]参阅

范围 for 循环(C++11) 执行范围上的循环[编辑]
应用函数到元素范围
(算法函数对象)[编辑]
应用函数对象到序列的前 N 个元素
(算法函数对象)[编辑]
应用一元函数对象范围中元素
(函数模板)[编辑]
close