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
TODOC++23
TODOC++20
TODOC++17
TODOC++14
TODOC++11
TODOSee also
TODO
This page was last modified on 2024-10-13