@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



See also

TODO

This page was last modified on 2024-10-13