Convert a String In C++ To Upper Case

后端 未结 30 1515
一个人的身影
一个人的身影 2020-11-22 05:25

How could one convert a string to upper case. The examples I have found from googling only have to deal with chars.

30条回答
  •  感情败类
    2020-11-22 06:07

    typedef std::string::value_type char_t;
    
    char_t up_char( char_t ch )
    {
        return std::use_facet< std::ctype< char_t > >( std::locale() ).toupper( ch );
    }
    
    std::string toupper( const std::string &src )
    {
        std::string result;
        std::transform( src.begin(), src.end(), std::back_inserter( result ), up_char );
        return result;
    }
    
    const std::string src  = "test test TEST";
    
    std::cout << toupper( src );
    

提交回复
热议问题