名稱空間
變體
操作

std::ranges::for_each, std::ranges::for_each_result

來自 cppreference.com
< cpp‎ | 演算法‎ | 範圍
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
受約束演算法
此選單中的所有名稱均屬於名稱空間 std::ranges
非修改序列操作
修改序列操作
劃分操作
排序操作
二分查詢操作(在已排序的範圍內)
       
       
集合操作(於已排序範圍上)
堆操作
最小/最大值操作
       
       
排列操作
摺疊操作
數值操作
(C++23)            
對未初始化儲存的操作
返回型別
 
定義於標頭檔案 <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 >
constexpr for_each_result<I, Fun>

    for_each( I first, S last, Fun f, Proj proj = {} );
(1) (C++20 起)
template< ranges::input_range R, class Proj = std::identity,

          std::indirectly_unary_invocable<
              std::projected<ranges::iterator_t<R>, Proj>> Fun >
constexpr for_each_result<ranges::borrowed_iterator_t<R>, Fun>

    for_each( R&& r, Fun f, Proj proj = {} );
(2) (C++20 起)
輔助型別
template< class I, class F >
using for_each_result = ranges::in_fun_result<I, F>;
(3) (C++20 起)
1) 按順序將給定的函式物件 f 應用於範圍 [firstlast) 中每個迭代器經投影后的值。
2)(1),但使用 r 作為源範圍,如同使用 ranges::begin(r) 作為 first 且使用 ranges::end(r) 作為 last

對於這兩個過載,如果迭代器型別是可變的,則 f 可以透過解引用迭代器修改範圍的元素。如果 f 返回結果,則結果被忽略。

本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即

目錄

[編輯] 引數

first, last - 定義要應用函式的元素範圍的迭代器-哨兵對
r - 要應用函式的元素範圍
f - 要應用於投影範圍的函式
proj - 應用於元素的投影

[編輯] 返回值

{std::ranges::next(std::move(first), last), std::move(f)}

[編輯] 複雜度

精確地對 fproj 應用 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) 在範圍內執行迴圈[編輯]
對一個範圍的元素應用函式
(演算法函式物件)[編輯]
對序列中的前 N 個元素應用函式物件
(演算法函式物件)[編輯]
範圍中的元素應用一元函式物件
(函式模板) [編輯]