Which implementation do you think is better?
std::string ToUpper( const std::string& source )
{
std::string result;
result.resize( 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.
EDIT 1. I cannot use boost lib. 2. I just wanted to compare between the allocation-during-constructor and the allocation after-constructor.