std::transform
: cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| (1) | ||
template< class InputIt, class OutputIt, class UnaryOperation > OutputIt transform( InputIt first1, InputIt last1, OutputIt d_first, UnaryOperation unary_op ); |
(C++20) | |
template< class InputIt, class OutputIt, class UnaryOperation > constexpr OutputIt transform( InputIt first1, InputIt last1, OutputIt d_first, UnaryOperation unary_op ); |
(C++20) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryOperation > ForwardIt2 transform( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 d_first, UnaryOperation unary_op ); |
(2) | (C++17) |
| (3) | ||
template< class InputIt1, class InputIt2, class OutputIt, class BinaryOperation > OutputIt transform( InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt d_first, BinaryOperation binary_op ); |
(C++20) | |
template< class InputIt1, class InputIt2, class OutputIt, class BinaryOperation > constexpr OutputIt transform( InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt d_first, BinaryOperation binary_op ); |
(C++20) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class ForwardIt3, class BinaryOperation > ForwardIt3 transform( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt3 d_first, BinaryOperation binary_op ); |
(4) | (C++17) |
std::transform d_first
1)
unary_op [first1, last1) 3)
binary_op [first1, last1) first2 22,4) (1,3)
policy std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> true
|
|
(C++11) |
|
|
(C++11) |
| first1, last1 | - | 1 |
| first2 | - | 2 |
| d_first | - | first1 first2
|
| policy | - | |
| unary_op | - |
|
| binary_op | - |
|
-InputIt, InputIt1, InputIt2 LegacyInputIterator
| ||
-OutputIt LegacyOutputIterator
| ||
-ForwardIt1, ForwardIt2, ForwardIt3 LegacyForwardIterator
| ||
1-2)
std::distance(first1, last1) unary_op 3-4)
std::distance(first1, last1) binary_op ExecutionPolicy
-
ExecutionPolicystd::terminateExecutionPolicy - std::bad_alloc
| 1 |
|---|
template<class InputIt, class OutputIt, class UnaryOperation>
OutputIt transform(InputIt first1, InputIt last1, OutputIt d_first,
UnaryOperation unary_op)
{
while (first1 != last1) {
*d_first++ = unary_op(*first1++);
}
return d_first;
}
|
| 2 |
template<class InputIt1, class InputIt2,
class OutputIt, class BinaryOperation>
OutputIt transform(InputIt1 first1, InputIt1 last1, InputIt2 first2,
OutputIt d_first, BinaryOperation binary_op)
{
while (first1 != last1) {
*d_first++ = binary_op(*first1++, *first2++);
}
return d_first;
}
|
std::transform unary_op binary_op std::for_each
toupper transform
Run this code
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::string s("hello");
std::transform(s.begin(), s.end(), s.begin(),
[](unsigned char c) -> unsigned char { return std::toupper(c); });
std::vector<std::size_t> ordinals;
std::transform(s.begin(), s.end(), std::back_inserter(ordinals),
[](unsigned char c) -> std::size_t { return c; });
std::cout << s << ':';
for (auto ord : ordinals) {
std::cout << ' ' << ord;
}
}
:
HELLO: 72 69 76 76 79
| () |