string타입 문자열 전체를 대문자로 바꾸는 방법

조회수 12279회

한 번에 스트링 전체를 대문자로 바꾸는 방법이 있을까요? 제가 구글에서 찾은 건 char형인데 저는 string형에서 써야 합니다

1 답변

  • 좋아요

    0

    싫어요
    채택 취소하기

    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");
    

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)