GeezerVison is showing cpprefjp.github.io — std::for_each_n - cpprefjp C++日本語リファレンス
最終更新日時:
2026年04月13日 15時17分49秒 (JST)
teruyamato0731
が更新
履歴
編集
function template
<algorithm>
std:: for_each_n (C++17)
namespace std {
template < class InputIterator , class Size , class Function >
InputIterator
for_each_n ( InputIterator first ,
Size n ,
Function f ); // (1) C++17
template < class InputIterator , class Size , class Function >
constexpr InputIterator
for_each_n ( InputIterator first ,
Size n ,
Function f ); // (1) C++20
template < class ExecutionPolicy , class ForwardIterator , class Size , class Function >
ForwardIterator
for_each_n ( ExecutionPolicy && exec ,
ForwardIterator first ,
Size n ,
Function f ); // (2) C++17
}
概要
イテレータ範囲[first, first + n) (範囲の先頭N要素) のすべての要素に、指定された関数を適用する。
要件
Function は MoveConstructible の要件を満たす必要があるが、CopyConstructible の要件を満たす必要はない
n >= 0であること
効果
イテレータ範囲[first, first + n) 内の全てのイテレータ i に f(*i) という操作を行う。
このアルゴリズムはその他のアルゴリズムと違い、関数 f の内部で *i の値を書き換えても構わない(もちろんイテレータの型が mutable iterator の要件を満たしている場合に限る)。
戻り値
備考
例
出力
6
2
8
処理系
関連項目
参照