std::ranges...each_result(3)
π std::ranges::for_each, std::ranges::for_each_result π
Okay bestie, this is the std::ranges...each_result(3) page from the C++ Standard Libary manual β yes, the header spells it like that, and we're keeping the whole vibe intact. This is the main-character page for std::ranges::for_each and std::ranges::for_each_result, and every detail is staying right here. π«Ά
πΈ NAME πΈ
std::ranges::for_each,std::ranges::for_each_result β std::ranges::for_each,std::ranges::for_each_result
π Synopsis π
Defined in header <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) (since C++20)
constexpr for_each_result<I, Fun>
for_each( I first, S last, Fun f, Proj proj = {} );
template< ranges::input_range R, class Proj = std::identity,
std::indirectly_unary_invocable<
std::projected<ranges::iterator_t<R>, Proj>> Fun >
(2) (since C++20)
constexpr for_each_result<ranges::borrowed_iterator_t<R>, Fun>
for_each( R&& r, Fun f, Proj proj = {} );
π§ Helper types π§
template< class I, class F >
(3) (since C++20)
using for_each_result = ranges::in_fun_result<I, F>;
- Applies the given function object
fto the result of the value projected by each iterator in the range[first, last), in order. β¨ - Same as (1), but uses
ras the source range, as if usingranges::begin(r)asfirstandranges::end(r)aslast. π
For both overloads, if the iterator type is mutable, f may modify the elements of the range through the dereferenced iterator. If f returns a result, the result is ignored. Noted, bestie.
The function-like entities described on this page are niebloids, that is:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
In practice, they may be implemented as function objects, or with special compiler extensions.
π« Parameters π«
first,lastβ iterator-sentinel pair denoting the range to apply the function to.rβ the range of elements to apply the function to.fβ the function to apply to the projected range.projβ projection to apply to the elements.
π₯ Return value π₯
{std::ranges::next(std::move(first), last), std::move(f)}
β Complexity β
Exactly last - first applications of f and proj.
π§ Possible implementation π§
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;
π¦ Example π¦
The following example uses a lambda expression to increment all of the elements of a vector and then uses an overloaded operator() in a functor to compute their sum. Note that to compute the sum, it is recommended to use the dedicated algorithm std::accumulate. π
// Run this code
#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');
}
π Output π
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
π See also π
range-for loop(C++11)β executes loop over rangeranges::transformβ applies a function to a range of elements(C++20)(niebloid)ranges::for_each_nβ applies a function object to the first N elements of a sequence(C++20)(niebloid)for_eachβ applies a function to a range of elements(function template)
source/version/date check, bestie: http://cppreference.com Β· 2024.06.10 Β· page std::ranges...each_result(3) β that's the footer, and we are out. Byeee π
π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms