std::ranges::for_each, std::ranges::for_each_result
來自 cppreference.com
定義於標頭檔案 <algorithm> |
||
呼叫簽名 (Call signature) |
||
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 起) |
1) 按順序將給定的函式物件 f 應用於範圍
[
first,
last)
中每個迭代器經投影后的值。對於這兩個過載,如果迭代器型別是可變的,則 f 可以透過解引用迭代器修改範圍的元素。如果 f 返回結果,則結果被忽略。
本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即
目錄 |
[編輯] 引數
first, last | - | 定義要應用函式的元素範圍的迭代器-哨兵對 |
r | - | 要應用函式的元素範圍 |
f | - | 要應用於投影範圍的函式 |
proj | - | 應用於元素的投影 |
[編輯] 返回值
{std::ranges::next(std::move(first), last), std::move(f)}
[編輯] 複雜度
精確地對 f 和 proj 應用 last - first 次。
[編輯] 可能實現
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 表示式遞增向量的所有元素,然後使用函式物件中過載的 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 迴圈(C++11) |
在範圍內執行迴圈 |
(C++20) |
對一個範圍的元素應用函式 (演算法函式物件) |
(C++20) |
對序列中的前 N 個元素應用函式物件 (演算法函式物件) |
對範圍中的元素應用一元函式物件 (函式模板) |