@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



See also

TODO

This page was last modified on 2024-03-21