@specsoftdev live:.cid.8e17e9b93cabb607 specsoftdev@gmail.com
std::for_each_n
Актуально для C++23.

#include <algorithm>
Актуально на 2024-10-14.



Define overload #1
template<class InputIterator, class Size, class Function>
constexpr InputIterator for_each_n(InputIterator first, Size n, Function f);

Применяет унарный предикат "f" к каждому элементу из диапазона [first, first + n].
Вернёт итератор first + n.
Example, possible implementation
Define overload #2
template<class ExecutionPolicy, class ForwardIterator, class Size, class Function>
ForwardIterator for_each_n(ExecutionPolicy&& exec, ForwardIterator first, Size n, Function f);

TODO
Example, possible implementation


Examples


Example 1:
#include <iostream>
#include <algorithm>
#include <vector>

template<class T>
auto print(const std::vector<T>& v)
{
    for (auto c : v)
        std::cout << c << ' ';
}

int main()
{
    std::vector<int> vec{0,1,2,3,4,5,6,7,8,9};
    std::for_each_n(std::begin(vec), std::size(vec), [](auto& data) {
        data *= 10;
    });
    print(vec);
}

0 10 20 30 40 50 60 70 80 90



Changelog

C++26
TODO
C++23
TODO
C++20
TODO
C++17
TODO
C++14
TODO
C++11
TODO


See also

TODO

This page was last modified on 2024-10-14