std::ranges::for_each_n, std::ranges::for_each_n_result
ja.cppreference.net より
| ヘッダ <algorithm>
|
||
| で定義 |
||
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>
for_each_n( I first, std::iter_difference_t<I> count,
Fun f, Proj proj = {} );
|
(1) | (C++20以降) |
template< /*execution-policy*/ Ep,
std::random_access_iterator I, class Proj = identity,
std::indirectly_unary_invocable<std::projected<I, Proj>> Fun >
I for_each_n( Ep&& policy, I first, iter_difference_t<I> count,
Fun f, Proj proj = {} );
|
(2) | (C++26以降) |
| ヘルパー型 |
||
template< class I, class F >
using for_each_n_result = ranges::in_fun_result<I, F>;
|
(3) | (C++20以降) |
定義 /*execution-policy*/については、このページを参照してください。
指定された呼び出し可能オブジェクト f を、ターゲット範囲 proj 内の各要素 ([first, ranges::next(first, count)) によって射影されたもの) に適用します。f が結果を返す場合、その結果は無視されます。
1)
f は first の順序で適用されます。2)
f は順序通りに適用されない可能性があります。アルゴリズムは policy に従って実行されます。 他の 並列アルゴリズムとは異なり、
for_each_n はターゲット範囲の要素の任意のコピーを作成することは許可されていません。count >= 0 が true でない場合、動作は未定義です。
このページに記載されている関数のようなエンティティは アルゴリズム関数オブジェクト (非公式に niebloids として知られています) であり、つまり:
- それらのいずれかを呼び出す際に、明示的なテンプレート引数リストを指定することはできません。
- それらのいずれも 引数依存の名前検索では見えません。
- 関数呼び出し演算子の左側の名前として 通常の修飾なし名前検索によってそれらのいずれかが見つかった場合、引数依存の名前検索は抑制されます。
引数
| first | - | ターゲット範囲の先頭 |
| count | - | ターゲット範囲内の要素数 |
| f | - | (射影された)要素に適用される呼び出し可能オブジェクト |
| proj | - | 要素に適用される射影 |
| policy | - | 使用する実行ポリシー |
戻り値
1)
{ranges::next(first, count), std::move(f)}2)
ranges::next(first, count)計算量
正確にcount回のfとprojの適用。
例外
2) 実行処理中:
- 並列化に必要な一時的なメモリ資源が利用できない場合、std::bad_alloc が送出される。
- アルゴリズムの引数を介してオブジェクトにアクセス中に、捕捉されない例外が送出された場合の動作は実行ポリシーにより決定される(標準ポリシーでは、std::terminate が呼び出される)。
注記
射影が変更可能な参照を返す場合、fはターゲット範囲の要素を変更できる可能性があります。
実装例
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> count,
Fun fun, Proj proj = Proj{}) const
{
for (; count-- > 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, const auto& 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);
// Negate first three numbers:
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);
// Negate data members “P::first” using projection:
std::ranges::for_each_n(s.begin(), 2, [](auto& x) { x *= -1; }, &P::first);
print("s", s);
// Capitalize data members “P::second” using projection:
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'}
関連項目
(C++17) |
シーケンスの先頭N個の要素に関数オブジェクトを適用する (関数テンプレート) |
| 単項関数オブジェクトを範囲 の各要素に適用する&(関数テンプレート | |
ranges::for_each |
|
(C++20)for範囲forループ
|
(C++11) |