std::ranges::for_each 定义于头文件<algorithm> template<std::input_iteratorI, std::sentinel_for<I>S,classProj=std::identity, std::indirectly_unary_invocable<std::projected<I, Proj>>Fun> constexprranges::for_each_result<I, Fun>for_each(I first, S last, Fun f, Proj proj={}); ...
{std::ranges::next(std::move(first), last), std::move(f)} Complexity Exactlylast-firstapplications offandproj. Possible implementation structfor_each_fn{template<std::input_iteratorI,std::sentinel_for<I>S,classProj=std::identity,std::indirectly_unary_invocable<std::projected<I, Proj>>Fun>...
size(); const auto& e : v) std::cout << e << (--n ? ", " : "\n"); }; int main() { std::array a {1, 2, 3, 4, 5}; print("a", a); // Negate first three numbers: std::ranges::for_each_n(a.begin(), 3, [](auto& n) { n *= -1; }); print("a",...