string타입 문자열 전체를 대문자로 바꾸는 방법
조회수 11964회
1 답변
-
1. std::transform을 쓰는 방법
#include <algorithm> #include <string> std::string str = "Hello World"; std::transform(str.begin(), str.end(),str.begin(), ::toupper);
헤더에 있으며, 범위에 있는 모든 원소에 대해 function을 수행한 결과를 다른 범위에 저장합니다.
원형은
template< class InputIt, class OutputIt, class UnaryOperation > OutputIt transform( InputIt first1, InputIt last1, OutputIt d_first, UnaryOperation unary_op ); //또는 template< class InputIt1, class InputIt2, class OutputIt, class BinaryOperation > OutputIt transform( InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt d_first, BinaryOperation binary_op );
입니다.
파라미터는
first1
,last1
:transform
할 첫 번째 원소first2
:transform
할 2번째 범위의 첫 번째 원소d_first
:transform
결과를 저장할 범위의 첫 번째 위치unary_op
: 객체를 바꾸는unary
함수bianry_op
: 객체를 바꾸는binary
함수
입니다
2. Boost를 쓰는 방법
#include <boost/algorithm/string.hpp> #include <string> std::string str = "Hello World"; boost::to_upper(str); std::string newstr = boost::to_upper_copy<std::string>("Hello World");
댓글 입력