TITLE: toupper for strings of arbitrary character types (Source: comp.lang.c++, 18 Oct 2000) SEBASTIAN: Josh Sebastian (tar_curien@my-deja.com) :>: int main() :>: { :>: std::string s = "Hello, world!"; :>: std::transform(s.begin(), s.end(), s.begin(), std::toupper); RODRIGUEZ: Fernando Rodríguez (spamers@must.die) :>The above line should read :> :> std::transform(s.begin(), s.end(), s.begin(), :> static_cast(std::toupper)); UNKNOWN: : Would this same technique work on unicode strings (wstring)? O:-) KUEHL: kuehl@ramsen.informatik.uni-konstanz.de (Dietmar Kuehl) Yes. It is written simpler though: std::transform(s.begin(), s.end(), s.begin(), std::towupper); The cast is not needed here because there is only one verson of 'std::towupper' and thus there is no ambiguity. Basically, the same approach can be extended to arbitrary character types at the cost of writing a simple template function: template T to_upper(T c) { return std::use_facet >(std::locale()).toupper(c); } std::transform(s.begin(), s.end(), s.begin(), to_upper); Of course, it is necessary to implement and install the corresponding 'std::ctype' facets for all types 'T' different than 'char' and 'wchar_t'. ... and the performance of the above code sucks! It is not that bad with the other approach. _______________________________________________ cpptips mailing list http://cpptips.hyperformix.com