alfps
12/17/2008 11:37:00 PM
On 17 Des, 22:34, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
> Michal wrote:
>
> > Hallo
> > I looked through ANSI/ISO C++ standard string, and I did not find any
> > function from string class that would do so. Did I overlooked
> > something or it is so?
>
> Maybe something like:
>
> #include <locale>
> #include <string>
>
> template < typename Char, typename Traits >
> std::basic_string< Char, Traits > &
> to_lower ( std::basic_string< Char, Traits > & str,
> std::locale loc = std::locale() ) {
> typedef std::basic_string< Char, Traits > string;
> typedef std::ctype< Char > char_type;
> char_type const * the_type_ptr = &std::use_facet< char_type >( loc );
> for ( typename string::size_type i = 0; i < str.size(); ++i ) {
> str[i] = the_type_ptr->tolower( str[i] );
> }
> return ( str );
>
> }
This is indeed the *intended* way for C++ level conversion.
Unfortunately MinGW g++ 3.4.5 for Windows lacks locale support, hence
the above won't uppercase e.g. Norwegian characters.
The practical solution is to use the C library's toupper function
(MinGW g++ uses Microsoft's runtime library which does this
correctly).
Others have posted such code that won't work in general.
It's important to remember to add a setlocale( "", LC_ALL ) (or
perhaps the arguments are in the opposite order, check).
>
> #include <iostream>
> #include <ostream>
>
> int main ( void )
This void is a C-ism, best a-voided in C++.
> {
> std::string msg ( "Hello World!" );
> to_lower( msg );
> std::cout << msg << '\n';
>
> }
Cheers & hth.,
- Alf