std::for_each_n(3)
π NAME π
okay bestie, welcome to std::for_each_n(3) from the C++ Standard Libary β yes, thatβs the manual name, and yes the header spells it βLibary,β we are leaving it exactly like that, iconic behavior π
. The NAME section is literally:
std::for_each_n - std::for_each_n
no cap, thatβs the whole name section, sheβs concise and we respect it.
β¨ Synopsis β¨
Defined in header <algorithm> β got it, bestie:
template< class InputIt, class Size, class UnaryFunc > (since C++17)
InputIt for_each_n( InputIt first, Size n, UnaryFunc f (1)
(constexpr since C++20)
);
template< class ExecutionPolicy,
class ForwardIt, class Size, class UnaryFunc >
ForwardIt for_each_n( ExecutionPolicy&& policy, (2) (since C++17)
ForwardIt first, Size n, UnaryFunc
f );
so what does it do? It applies the given function object f to the result of dereferencing every iterator in the range [first, first + n). If f returns a result, the result is ignored β the algorithm is lowkey ghosting that return value, and honestly? same.
fis applied in order starting fromfirst. IfUnaryFuncis notMoveConstructible, the behavior is undefined. thatβs UB, bestie β do not play.fmight not be applied in order. The algorithm is executed according topolicy. This overload participates in overload resolution only ifstd::is_execution_policy_v<std::decay_t<ExecutionPolicy>>is true (until C++20), orstd::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>is true (since C++20).
If UnaryFunc is not CopyConstructible, the behavior is undefined. If n >= 0 is not true, the behavior is undefined. literally check your n, fr fr.
If the iterator type (InputIt/ForwardIt) is mutable, f may modify the elements of the range through the dereferenced iterator.
Unlike the rest of the parallel algorithms, for_each_n is not allowed to make copies of the elements in the sequence even if they are TriviallyCopyable. no sneaky copies, not even a little π
πΈ Parameters πΈ
firstβ the beginning of the range to apply the function to.nβ the number of elements to apply the function to.policyβ the execution policy to use. See execution policy for details.fβ function object, to be applied to the result of dereferencing every iterator in the range[first, first + n).- The signature of the function should be equivalent to the following:
void fun(const Type &a); - The signature does not need to have
const &. - The type
Typemust be such that an object of typeInputItcan be dereferenced and then implicitly converted toType.
- The signature of the function should be equivalent to the following:
π¦ Type requirements π¦
InputItmust meet the requirements ofLegacyInputIterator.ForwardItmust meet the requirements ofLegacyForwardIterator.Sizemust be convertible to an integral type.
π Return value π
An iterator equal to first + n, or more formally, to std::advance(first, n). she returns the iterator, periodt.
β Complexity β
Exactly n applications of f.
π₯ Exceptions π₯
The overload with a template parameter named ExecutionPolicy reports errors as follows:
- If execution of a function invoked as part of the algorithm throws an exception and
ExecutionPolicyis one of the standard policies,std::terminateis called. For any otherExecutionPolicy, the behavior is implementation-defined. - If the algorithm fails to allocate memory,
std::bad_allocis thrown.
π§ Possible implementation π§
See also the implementation in libstdc++, libc++ and MSVC stdlib.
template<class InputIt, class Size, class UnaryFunc>
InputIt for_each_n(InputIt first, Size n, UnaryFunc f)
{
for (Size i = 0; i < n; ++first, (void) ++i)
f(*first);
return first;
}
π Example π
run this code, bestie:
// Run this code
#include <algorithm>
#include <iostream>
#include <vector>
void println(auto const& v)
{
for (auto count{v.size()}; const auto& e : v)
std::cout << e << (--count ? ", " : "\n");
}
int main()
{
std::vector<int> vi{1, 2, 3, 4, 5};
println(vi);
std::for_each_n(vi.begin(), 3, [](auto& n) { n *= 2; });
println(vi);
}
π₯οΈ Output π₯οΈ
1, 2, 3, 4, 5
2, 4, 6, 4, 5
π See also π
transformβ applies a function to a range of elements, storing results in a destination range (function template)range-for loop(C++11)β executes loop over rangefor_eachβ applies a function to a range of elements (function template)ranges::for_each_n(C++20) β applies a function object to the first N elements of a sequence (niebloid)
source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::for_each_n(3) π«Άβ¨
π¦ 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