std::ranges:: for_each, std::ranges:: for_each_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,
std::
sentinel_for
<
I
>
S,
class
Proj
=
std::
identity
,
std::
indirectly_unary_invocable
<
std
::
projected
<
I, Proj
>>
Fun
>
|
(1) | (C++20以降) |
|
template
<
ranges::
input_range
R,
class
Proj
=
std::
identity
,
std::
indirectly_unary_invocable
<
|
(2) | (C++20以降) |
|
ヘルパー型
|
||
|
template
<
class
I,
class
F
>
using for_each_result = ranges:: in_fun_result < I, F > ; |
(3) | (C++20以降) |
[
first
,
last
)
内の各イテレータによって投影された値の結果に順番に適用します。
両方のオーバーロードにおいて、イテレータ型がミュータブルである場合、 f はデリファレンスされたイテレータを通じて範囲の要素を変更することができます。 f が結果を返す場合、その結果は無視されます。
このページで説明されている関数ライクなエンティティは、 アルゴリズム関数オブジェクト (非公式には niebloids として知られる)です。すなわち:
- 明示的なテンプレート引数リストは、いずれかを呼び出す際に指定することはできません。
- いずれも 実引数依存の名前探索 では可視になりません。
- いずれかが関数呼び出し演算子の左側の名前として 通常の非修飾名前探索 によって発見された場合、 実引数依存の名前探索 は抑制されます。
目次 |
パラメータ
| first, last | - | 関数を適用する要素の 範囲 を定義するイテレータ-センチネルペア |
| r | - | 関数を適用する要素の範囲 |
| f | - | 投影された範囲に適用する関数 |
| proj | - | 要素に適用する投影 |
戻り値
{ ranges:: next ( std :: move ( first ) , last ) , std :: move ( f ) }
計算量
ranges:: distance ( first, last ) の正確な回数だけ f および proj を適用します。
実装例
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)); } }; inline constexpr for_each_fn for_each; |
例
以下の例では、
lambda expression
を使用してベクターの全要素をインクリメントし、その後ファンクタ内でオーバーロードされた
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 = [](const auto& n) { std::cout << ' ' << n; }; namespace ranges = std::ranges; std::cout << "before:"; ranges::for_each(std::as_const(nums), print); print('\n'); ranges::for_each(nums, [](int& n) { ++n; }); // calls Sum::operator() for each number auto [i, s] = ranges::for_each(nums.begin(), nums.end(), Sum()); assert(i == nums.end()); std::cout << "after: "; ranges::for_each(nums.cbegin(), nums.cend(), print); std::cout << "\n" "sum: " << s.sum << '\n'; using pair = std::pair<int, std::string>; std::vector<pair> pairs {{1,"one"}, {2,"two"}, {3,"tree"}}; std::cout << "project the pair::first: "; ranges::for_each(pairs, print, [](const pair& p) { return p.first; }); std::cout << "\n" "project the pair::second:"; ranges::for_each(pairs, print, &pair::second); print('\n'); }
出力:
before: 3 4 2 8 15 267 after: 4 5 3 9 16 268 sum: 305 project the pair::first: 1 2 3 project the pair::second: one two tree
関連項目
range-
for
loop
(C++11)
|
範囲に対するループを実行 |
|
(C++20)
|
範囲の要素に関数を適用
(アルゴリズム関数オブジェクト) |
|
(C++20)
|
シーケンスの先頭N個の要素に関数オブジェクトを適用
(アルゴリズム関数オブジェクト) |
|
単項
関数オブジェクト
を
範囲
の要素に適用
(関数テンプレート) |