about img
blogger img

UnderPaidLoveMonkis posts img

scotts posts image

buchos posts img

Corys posts image


How can C++ Not have a toUpper/toLower?

Scott Rippee @ 11:36 pm May 11th, 2006

With all of C++ and STL's functionality how can toUpper and toLower not exist for strings? I know there are plenty of similar points that can be brought up about c++ or any other language, but this is something that would definately be useful. What is more ridiculous is some of the examples I've found about how this can be done, but with no definitive best solution.

C++:
  1. std::transform( s.begin(), s.end(), s.begin(),
  2. std::bind1st( std::mem_fun( &std::ctype<char>::tolower ),
  3. &std::use_facet<std::ctype<char>>( loc ) ) );

or

C++:
  1. static_cast<char(std::ctype<char>::*)(char) const>(
  2. &std::ctype<char>::tolower)

These are both pretty horrific looking in my opinion whether they are portable or not.

Still not as clean as string.toLpper() would be, but slightly "more pretty" is what I came up with, the char tolower wrapped in a string iterator loop:

C++:
  1. string foo = "MyStRiNG";
  2. for( string::iterator sit=foo.begin(); sit<=foo.end(); sit++)
  3. {
  4.    *sit = tolower(*sit);
  5. }

Leave a Reply

Subscribe without commenting