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

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



Define overload #1
template<class InputIter, class OutputIter, class UnaryOperation>
constexpr OutputIter
transform(InputIter first1, InputIter last1,
          OutputIter result, UnaryOperation op);

Применяет унарный предикат "op" к каждому элементу из диапазона [first1, last1] записывая результат в result.
Вернёт итератор "result" увеличенный на количество элементов записанных через него.
Example, possible implementation
Define overload #2
template<class ExecutionPolicy, class ForwardIter1, class ForwardIter2,
         class UnaryOperation>
ForwardIter2 transform(ExecutionPolicy&& exresultecresult,
          ForwardIter1 first1, ForwardIter1 last1,
          ForwardIter2 result, UnaryOperation op);

TODO
Example, possible implementation
Define overload #3
template<class InputIter1, class InputIter2,
class OutputIter, class BinaryOperation>
constexpr OutputIter
transform(InputIter1 first1, InputIter1 last1,
          InputIter2 first2, OutputIter result,
          BinaryOperation binary_op);

Применяет бинарный предикат "op" к каждому элементу из диапазона [first1, last1] и [first2, ], записывая результат в result.
Вернёт OutputIter итератор увеличенный на количество скопированных элементов.
Example, possible implementation
Define overload #4
template<class ExecutionPolicy, class ForwardIter1, class ForwardIter2,
         class ForwardIter, class BinaryOperation>
ForwardIter
transform(ExecutionPolicy&& exec,
          ForwardIter1 first1, ForwardIter1 last1,
          ForwardIter2 first2, ForwardIter result,
          BinaryOperation binary_op);

TODO
Example, possible implementation


Examples


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

int main()
{
    std::vector<int> v1 {1,2,3,4,5};
    std::vector<int> v2(v1.size());
    std::vector<int> v3(v1.size());

    std::transform(std::begin(v1), std::end(v1), std::begin(v2),
                   [](auto val) { return val*10; });

    std::transform(std::begin(v1), std::end(v1),
                   std::begin(v2),
                   std::begin(v3), std::plus<>{});

    const auto print([](auto const& v) {
        for (auto it : v) {
            std::cout << it << ' ';
        }
        std::cout << std::endl;
    });

    print(v2);
    print(v3);

    return 0;
}


10 20 30 40 50
11 22 33 44 55



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-03-21