언어/Basic C++

[Basic C++] #73_transform 알고리즘

Hardii2 2024. 4. 17. 14:18

 

#1. 개념

 

1. 정의

C++의 표준 라이브러리에서 제공하는 transform 함수는 주어진 컨테이너 목록을 순회하며 각 요소에 대해 지정된 연산을 적용한 결과를 다른 컨테이너에 저장할 수 있도록 해줍니다.

 

2. 헤더

#includ <algorithm>

 

3. syntax 

template< class InputIt, class OutputIt, class UnaryOperation >
OutputIt transform(InputIt first1, InputIt last1, OutputIt d_first, UnaryOperation unary_op);
first1, last1은 지정된 연산을 적용할 첫 번째 범위의 시작과 끝을 가리키는 반복자입니다. [first1, last1)입니다. 
first2는 두 번째 범위의 시작을 가리키는 반복자입니다. 주어진 컨테이너 목록의 각 항목에 지정된 연산을 적용한 결과 값을 저장할 시작 위치입니다. 

 

#2. 코드

 

1. tolower, toupper

#include <string>
#include <algorithm> // transform
#include <cctype> // toupper, tolower

int main()
{
    string str = "adsfWFNDKsdf";
    
    transform(begin(str), end(str), begin(str), ::toupper);

    return 0;
}
위 예제 코드는 transform을 활용하여 string 유형의 변수의 각 문자를 순회하며 대문자로 변경하는 작업을 나타냅니다.