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); // 最初の3つの数値を反転: 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'}
関連項目
range-
for
ループ
(C++11)
|
範囲に対するループを実行 |
|
(C++20)
|
単項
関数オブジェクト
を
範囲
の要素に適用
(アルゴリズム関数オブジェクト) |
|
(C++17)
|
関数オブジェクトをシーケンスの最初のN個の要素に適用
(関数テンプレート) |
|
単項
関数オブジェクト
を
範囲
の要素に適用
(関数テンプレート) |