algorithm을 include한다.
#include <algorithm>
원형 - 단항 함수형:
template <class InputIt, class OutputIt, class UnaryOperation>
OutputIt transform(InputIt first, InputIt last,
OutputIt d_first, UnaryOperation unary_op);
원형 - 이항 함수형:
template <class InputIt1, class InputIt2, class OutputIt, class BinaryOperation>
OutputIt transform(InputItr1 first1, InputItr1 last1, InputItr2 first2,
OutputIt d_first, BinaryOperation binary_op);
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
//binary_op
int twice(int n) {
return n*2;
}
int main(){
vector<int> vec = {1, 2, 3, 4, 5};
//before transform 출력
for(int i = 0; i < vec.size(); i++){
cout << vec[i];
}
cout << endl;
transform(vec.begin(), vec.end(), vec.begin(), twice);
//after transform 출력
for(int i = 0; i < vec.size(); i++){
cout << vec[i];
}
return 0;
}
// 실행 결과: 1, 2, 3, 4, 5
// 1, 4, 9, 16, 25
int main(){
string str = "Hello World";
//before transform 출력
cout << str << endl;
transform(str.begin(), str.end(), str.begin(), ::toupper);
//after transform 출력
cout << str << endl;
return 0;
}
// 실행 결과: Hello World
// HELLO WORLD
출처: https://artist-developer.tistory.com/28 [개발자 김모씨의 성장 일기:티스토리]