In Prata, C++ Primer Plus, ch. 16 (STL, etc.) one of the exercises (ex. 3, p. 948) is to write a function converting a string to upper case. This is obviously not hard, but since my solution differs from his in the answers, I wondered which one you guys would view as superior:
My solution:
Code:
void str_toupper(std::string& s)
{
std::transform(s.begin(), s.end(), s.begin(), std::toupper);
}
My code does require the <algorithm> header, which his doesn't:
Code:
void ToUpper(string& str)
{
for (int i = 0; i < str.size(); i++)
str[i] = toupper(str[i]);
}
Which version is better? Is one or the other going to be faster in handling a string of near maximum size, for example? Or is it just 6 of one, half-dozen of the other?
In terms of lines of code, I get 1 line shorter inside the function but do have to #include <algorithm> for an extra line at the top, so I view that as a tie unless the inclusion of algorithm adds unnecessary overhead...