// constexpr since C++ 20
template<class InputIt, class OutputIt, class UnaryOp>
OutputIt transform(InputIt first1, InputIt last1, OutputIt d_first, UnaryOp unary_op);
// constexpr since C++ 20
template<class InputIt1, class InputIt2, class OutputIt, class BinaryOp>
OutputIt transform(InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt d_first, BinaryOp binary_op);
// 두 개의 입력 범위는 동일한 길이를 가져야 하며, first1과 first2는 각각 그 범위의 시작 지점을 가리키는 입력 반복자
C++17 ~
template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryOp>
ForwardIt2 transform(ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 d_first, UnaryOp unary_op);
template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class ForwardIt3, class BinaryOp>
ForwardIt3 transform(ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt3 d_first, BinaryOp binary_op);
// 두 개의 입력 범위는 동일한 길이를 가져야 하며, first1과 first2는 각각 그 범위의 시작 지점을 가리키는 순방향 반복자
: 지정된 범위의 요소들을 변환하여 새로운 범위에 저장하고 새로운 범위의 끝을 반환합니다.
실행 정책은 std::execution 헤더에 포함되어있습니다.
- execution::seq : 순차 실행 (기본 값)
- execution::par : 병렬 실행 (멀티코어 시스템에서 작업이 병렬로 수행됩니다.)
- execution::par_unseq : 병렬 및 비순차 실행
<unary_op example>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int main() {
vector<int> numbers = {1, 4, 9, 16};
vector<double> results(numbers.size());
transform(numbers.begin(), numbers.end(), results.begin(), [](int x) {
return sqrt(x); // 제곱근을 구함
});
for (double result : results) {
cout << result << " ";
}
return 0;
}
결과값
<binary_op example>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
int main() {
vector<int> vec1 = { 1, 2, 3, 4 };
vector<int> vec2 = { 10, 20, 30, 40 };
vector<int> results(vec2.size());
transform(vec1.begin(), vec1.end(), vec2.begin(), results.begin(), plus<int>());
for (const int & num : results) {
cout << num << " ";
}
return 0;
}
결과값
