GeezerVison is showing cpprefjp.github.io — std::ranges::for_each - cpprefjp C++日本語リファレンス
最終更新日時:
2026年09月11日 13時06分31秒 (JST)
Akira Takahashi
が更新
履歴
編集
function template
<algorithm>
std::ranges:: for_each (C++20)
namespace std :: ranges {
template < input_iterator I ,
sentinel_for < I > S ,
class Proj = identity ,
indirectly_unary_invocable < projected < I , Proj >> Fun >
constexpr for_each_result < I , Fun >
for_each ( I first ,
S last ,
Fun f ,
Proj proj = {}); // (1) C++20
template < input_range R ,
class Proj = identity ,
indirectly_unary_invocable < projected < iterator_t < R > , Proj >> Fun >
constexpr for_each_result < borrowed_iterator_t < R > , Fun >
for_each ( R && r ,
Fun f ,
Proj proj = {}); // (2) C++20
template < execution-policy Ep ,
random_access_iterator I ,
sized_sentinel_for < I > S ,
class Proj = identity ,
indirectly_unary_invocable < projected < I , Proj >> Fun >
I for_each ( Ep && exec ,
I first ,
S last ,
Fun f ,
Proj proj = {}); // (3) C++26
template < execution-policy Ep ,
sized-random-access-range R ,
class Proj = identity ,
indirectly_unary_invocable < projected < iterator_t < R > , Proj >> Fun >
borrowed_iterator_t < R >
for_each ( Ep && exec ,
R && r ,
Fun f ,
Proj proj = {}); // (4) C++26
}
概要
範囲の全ての要素に、指定された関数を適用する。
(1): イテレータ範囲を指定する
(2): Rangeを直接指定する
(3): (1)の並列アルゴリズム版。実行ポリシーを指定する
(4): (2)の並列アルゴリズム版。実行ポリシーを指定する
テンプレートパラメータ制約
この他にFunはcopy_constructible のモデルであることが要求される。
効果
[first,last) 内の全てのイテレータ i に invoke (f, invoke (proj, *i)) という操作を行う。first から順番に処理し、last - 1 まで行う。
このアルゴリズムはその他のアルゴリズムと違い、invoke (proj, *i) が書き換え可能な参照であれば、関数 f の内部でその値を書き換えても構わない。
戻り値
計算量
正確に f を last - first 回適用する
備考
例
基本的な使い方
出力
3
1
4
----
3
2
6
並列アルゴリズムの例 (C++26)
出力
2 4 6 8 10
バージョン
言語
処理系
実装例
struct for_each_impl {
template < input_iterator I , sentinel_for < I > S , class Proj = identity , indirectly_unary_invocable < projected < I , Proj >> Fun >
requires copy_constructible < Fun >
constexpr for_each_result < I , Fun > operator ()( I first , S last , Fun f , Proj proj = {}) const {
for (; first != last ; ++ first ) {
invoke ( f , invoke ( proj , * first ));
}
return { move ( first ), move ( f )};
}
template < input_range R , class Proj = identity , indirectly_unary_invocable < projected < iterator_t < R > , Proj >> Fun >
requires copy_constructible < Fun >
constexpr for_each_result < borrowed_iterator_t < R > , Fun > operator ()( R && r , Fun f , Proj proj = {}) const {
return ( * this )( begin ( r ), end ( r ), move ( f ), ref ( proj ));
}
};
inline constexpr for_each_impl for_each ;
参照