std::experimental::ranges::for_each
出自cppreference.com
< cpp | experimental | ranges
template< InputIterator I, Sentinel<I> S, class Proj = ranges::identity, IndirectUnaryInvocable<projected<I, Proj>> Fun > |
(1) | (範圍 TS) |
template< InputRange R, class Proj = ranges::identity, IndirectUnaryInvocable<projected<ranges::iterator_t<R>, Proj>> Fun > |
(2) | (範圍 TS) |
1) 按順序,對範圍
[
first,
last)
中的每個迭代器解引用後調用投影 proj 的結果調用給定的函數對象 f(即 ranges::invoke(f, ranges::invoke(proj, *i)))。對於兩個重載,若迭代器類型可變,則 f 可以通過解引用後的迭代器修改範圍的元素。若 f 返回了結果,則忽略此結果。
不同於另外的算法,不允許 for_each
複製序列中的元素,即使它們為可平凡複製。
不同於 std::for_each(它只要求可移動構造 (MoveConstructible) ),這些函數要求 Fun
實現 CopyConstructible
。
儘管聲明描述如上,算法聲明的模板形參的實際數量和順序是未指定的。從而若在調用算法時使用顯式模板實參,則程序很可能不可移植。
目錄 |
[編輯] 參數
first, last | - | 要應用函數的範圍 |
r | - | 要應用函數的範圍 |
f | - | 應用到每個投射後的範圍中元素的可調用對象 |
proj | - | 應用到元素的投射 |
[編輯] 返回值
含有下列兩個成員的 tagged_pair
:
- 第一成員帶標籤
tag::in
,是源範圍的尾後迭代器(即比較等於哨位 last 的I
類型的迭代器)。 - 第二成員帶標籤
tag::fun
,(在該函數對象的所有應用後)從std::move(f)
初始化。
[編輯] 複雜度
恰好應用 last - first 次 f 和 proj。
[編輯] 可能的實現
template<InputIterator I, Sentinel<I> S, class Proj = ranges::identity, IndirectUnaryInvocable<ranges::projected<I, Proj>> Fun> auto for_each(I first, S last, Fun f, Proj proj = Proj{}) -> ranges::tagged_pair<tag::in(I), tag::fun(Fun)> { for (; first != last; ++first) ranges::invoke(f, ranges::invoke(proj, *first)); return {std::move(first), std::move(f)}; } |
[編輯] 示例
本節未完成 原因:暫無示例 |
[編輯] 參閱
應用一個函數到元素範圍 (函數模板) | |
範圍 for 循環 (C++11)
|
執行範圍上的循環 |
應用一元函數對象到範圍中元素 (函數模板) | |
(C++17) |
應用函數對象到序列的前 N 個元素 (函數模板) |