473,795 Members | 3,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

tolower used by transform

I was using the following code to convert the string to
lowercase.

string foo = "Some Mixed Case Text";
transform(foo.b egin(), foo.end(), foo.begin(), tolower);

I thought the above code is portable.

But, the following page has a different view on this:
http://lists.debian.org/debian-gcc/2.../msg00092.html

Can anybody comment on it ?
Also, I would like to know whether tolower template or function will be used
in the above code.
transform(foo.b egin(), foo.end(), foo.begin(), tolower);
Jul 19 '05 #1
3 31871
qa********@redi ffmail.com (qazmlp) wrote in message news:<db******* *************** ****@posting.go ogle.com>...
string foo = "Some Mixed Case Text";
transform(foo.b egin(), foo.end(), foo.begin(), tolower);

I thought the above code is portable.

But, the following page has a different view on this:
http://lists.debian.org/debian-gcc/2.../msg00092.html


Well that's a damn good question!

The problem is that most implementations of the standard C <ctype.h>
header define functions like toupper/tolower/etc as macros. To make it
work in STL algorithms, you have to include <cctype> header instead of
<ctype.h>. At least on my PC (Debian/gcc 3.3), <cctype> undefines all
tolower/etc macros and pulls ::tolower/::toupper/etc functions into
std namespace, so that your sample will work fine.

However, in general it is recommended to drop old C functions in favor
of new standard library functionality. In this particular case, you
may want use the ctype locale facet, i.e.

#include <locale>

// ..............

std::locale loc;
char s[] = "Test String";
std::use_facet< std::ctype<char > >( loc ).tolower( s, s + sizeof(s)
);

Too bad it does not work for std::string, i.e the following code will
not compile:

std::locale loc;
string s = "Test String";
std::use_facet< std::ctype<char > >( loc ).tolower( s.begin(),
s.end() );

because std::ctype::tol ower() definition has only two variants:

char_type tolower(char_ty pe __c) const;
const char_type* tolower(char_ty pe* __lo, const char_type* __hi)
const;

This leads me to the following piece of code:

std::transform( s.begin(), s.end(), s.begin(),
std::bind1st( std::mem_fun( &std::ctype<cha r>::tolower ),
&std::use_facet < std::ctype<char > >( loc ) ) );

Nice, eh? :-)

Now it's truly C++, but I am not sure if I want to use such thing
instead of good old tolower() from <cctype>. Can anyone suggest a
better solution?

PS. <locale> header also defines a standalone std::tolower() function
that takes locale as a second parameter, but I don't know if it can be
used in transform, because it is a template/inline function, i.e.
std::bind2nd and std::ptr_fun do not work with it.

PPS. It would be REALLY great to hear other opinions on this subject!

Thanks,
Sergei.

--
Sergei Matusevich,
Brainbench MVP for C++
http://www.brainbench.com
Jul 19 '05 #2
On 22 Jul 2003 19:01:15 -0700, mo****@yahoo.co m (Sergei Matusevich)
wrote:
qa********@red iffmail.com (qazmlp) wrote in message news:<db******* *************** ****@posting.go ogle.com>...
string foo = "Some Mixed Case Text";
transform(foo.b egin(), foo.end(), foo.begin(), tolower);

I thought the above code is portable.

But, the following page has a different view on this:
http://lists.debian.org/debian-gcc/2.../msg00092.html
Well that's a damn good question!

The problem is that most implementations of the standard C <ctype.h>
header define functions like toupper/tolower/etc as macros. To make it
work in STL algorithms, you have to include <cctype> header instead of
<ctype.h>. At least on my PC (Debian/gcc 3.3), <cctype> undefines all
tolower/etc macros and pulls ::tolower/::toupper/etc functions into
std namespace, so that your sample will work fine.


I'm not sure a conforming C++ implementation can have macro versions
of the ctype.h headers. Most versions I have seen have #ifdef __cpp__
or similar, using inline functions for the C++ version and macros for
the C one.
However, in general it is recommended to drop old C functions in favor
of new standard library functionality. In this particular case, you
may want use the ctype locale facet, i.e.

#include <locale>

// ..............

std::locale loc;
char s[] = "Test String";
std::use_facet< std::ctype<char > >( loc ).tolower( s, s + sizeof(s)
);

Too bad it does not work for std::string, i.e the following code will
not compile:

std::locale loc;
string s = "Test String";
std::use_facet< std::ctype<char > >( loc ).tolower( s.begin(),
s.end() );

because std::ctype::tol ower() definition has only two variants:

char_type tolower(char_ty pe __c) const;
const char_type* tolower(char_ty pe* __lo, const char_type* __hi)
const;

This leads me to the following piece of code:

std::transform( s.begin(), s.end(), s.begin(),
std::bind1st( std::mem_fun( &std::ctype<cha r>::tolower ),
&std::use_facet < std::ctype<char > >( loc ) ) );

Nice, eh? :-)
tolower is overloaded so you can't take its address as you are trying
above, since you don't say which overload you want. You'd need
something like:

static_cast<cha r(std::ctype<ch ar>::*)(char) const>(
&std::ctype<cha r>::tolower)

Now it's truly C++, but I am not sure if I want to use such thing
instead of good old tolower() from <cctype>. Can anyone suggest a
better solution?
Converting a string to lower case can involve changing the length of
the string in some languages, and a general solution is going to be
quite complicated and involve complex heuristics. In english though,
in place modification is of course possible, and it is best to just
write a couple of functions that operate on strings. There are various
implementation possibilities, the simplest being an explicit loop.
PS. <locale> header also defines a standalone std::tolower() function
that takes locale as a second parameter, but I don't know if it can be
used in transform, because it is a template/inline function, i.e.
std::bind2nd and std::ptr_fun do not work with it.


Just because it is template and inline doesn't mean bind2nd won't work
(you just need to cast to choose the correct instantiation). However,
because it takes the locale argument by reference, it won't work since
bind2nd will attempt to form a reference to reference argument, which
is currently illegal.

Tom
Jul 19 '05 #3
Thank you Tom for a great posting!

to********@hotm ail.com (tom_usenet) wrote in message news:<3f******* ********@news.e asynet.co.uk>.. .
string foo = "Some Mixed Case Text";
transform(foo.b egin(), foo.end(), foo.begin(), tolower);

[...]
std::transform( s.begin(), s.end(), s.begin(),
std::bind1st( std::mem_fun( &std::ctype<cha r>::tolower ),
&std::use_facet < std::ctype<char > >( loc ) ) );
tolower is overloaded so you can't take its address as you are trying
above, since you don't say which overload you want. You'd need
something like:

static_cast<cha r(std::ctype<ch ar>::*)(char) const>(
&std::ctype<cha r>::tolower)
Not sure about other implementations of the standard library, but in
my gcc 3.3.1 tolower is non-virtual and it delegates all functionality
to the protected virtual do_tolower method. Therefore, taking address
of the tolower method is absolutely OK, I'm just not sure about its
portability [now after your posting :))].

[...]
Converting a string to lower case can involve changing the length of
the string in some languages, and a general solution is going to be
quite complicated and involve complex heuristics. In english though,
in place modification is of course possible, and it is best to just
write a couple of functions that operate on strings. There are various
implementation possibilities, the simplest being an explicit loop.


Good point! But then, as far as I understand, tolower from the
standard library is not a general solution because it's an "one char
in - one char out" implementation. From the other hand, I don't know
any locales that may require such a sophisticated case conversion
procedures.. :)

But the question remains open - what is the best (generic and
portable) way to do toupper/tolower conversion for an std::string (or
std::wstring or any std::basic_stri ng incarnation) in C++? Is there
any? :))

Thank you,
Sergei.

--
Sergei Matusevich,
Brainbench MVP for C++
http://www.brainbench.com
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
6032
by: David Rubin | last post by:
I get an error when I compile the following code: #include <algorithm> #include <cctype> #include <iostream> #include <string> using namespace std; string&
3
1821
by: Ahsan | last post by:
Hi All, I am having prolems with converting upper case characters to lower case in Unix's C. I saw the man pages and "tolower" function does it. But in my case its not working. My code is as follows: ============================================================ gets ( input ); sscanf(input, "%s", Cmd); for( p = 0; p < strlen( Cmd ); p++ )/* Changing all input to lower
11
3365
by: TheDD | last post by:
Hello, i don't manage to use the tolower() function: #include <algorithm> #include <iostream> #include <locale> #include <string> using std::cout;
2
1838
by: Daniel Aarno | last post by:
As far as I can tell the locale header should provide a tolower function however the following failes for me under gcc 3.4. transform(begin, end, begin2, std::tolower); with an error that the last arg is of unknown type. Any ideas on why?
4
3255
by: Eric Lilja | last post by:
Hello, consider this program: #include <iostream> #include <algorithm> #include <string> #include <cctype> int main() { std::string s = "HEJ";
13
3003
by: Soumen | last post by:
I wanted convert a mixed case string to a lower case one. And I tried following code: std::transform(mixedCaseString.begin(), mixedCaseString::end(), mixedCaseString.begin(), std::ptr_fun(tolower)); Even though I's including cctype and algorithm, I's getting compiler (g ++ 3.3.6) error: no matching function for call to `ptr_fun(<unknown type>)'
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10437
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.