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

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



Define overload #1
template<class InputIterator, class Function>
constexpr Function for_each(InputIterator first, InputIterator last, Function f);

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

TODO
Example, possible implementation



Examples


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

struct Func {
    std::string str;
    void operator()(int& ch)
    {
        str += std::to_string(ch) + ' ';
        ch *= 10;
    }
};

auto print(const auto& c) -> decltype(c.begin(), c.end(), void())
{
    for (auto i : c)
        std::cout << i << ' ';
}

int main()
{
    auto nine = 9;
    std::vector<int> vec{0,1,2,3,4,5,6,7,8};
    auto func = std::for_each(std::begin(vec), std::end(vec), Func{});
    func(nine);
    std::cout << func.str << std::endl;
    print(vec);
    return 0;
}

0 1 2 3 4 5 6 7 8 9
0 10 20 30 40 50 60 70 80



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-13