Making uppercase of std::string
- by Daniel K.
Which implementation do you think is better?
std::string ToUpper( const std::string& source )
{
std::string result;
result.reserve( source.length() );
std::transform( source.begin(), source.end(), result.begin(),
std::ptr_fun<int, int>( std::toupper ) );
return result;
}
and...
std::string ToUpper( const std::string& source )
{
std::string result( source.length(), '\0' );
std::transform( source.begin(), source.end(), result.begin(),
std::ptr_fun<int, int>( std::toupper ) );
return result;
}
Difference is that the first one uses reserve method after the default constructor, but the second one uses the constructor accepting the number of characters.