Ok, I wanted I nice way to convert a string to lower case, so I thought to myself "Why reinvent the wheel? A quick google search will surely reveal something useful.", so I had a quick look and was truly alarmed to see what most people seem to be doing and suggesting to others. It is basically this:
This looks bad to me, really bad! To my understanding it is attempting to overwrite the underlying const data buffer of the string in place! The version that I have implemented is as follows:Code:void StringToLower(std::string& str) { std::transform(str.begin(), str.end(), str.begin(), tolower); }
but this is grossly inefficient. Do any of you know I nice way to convert a string to lower case?Code:std::string StringToLower(std::string& str) { std::vector<char> vec(str.begin(), str.end()); std::transform(vec.begin(), vec.end(), vec.begin(), tolower); return std::string(vec.begin(),vec.end()); }




Reply With Quote