473,809 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with <algorithm> transform

Working on a Kubuntu 64bit system "c++ (GCC) 4.0.3".

The following simple program extracted from p.497 & 499 of N.M.Josurris'
"The C++ Standard Library ... " (file t.cpp):

1 #include <string>
2 #include <iostream>
3 #include <algorithm>
4 #include <cctype>
5 using namespace std;
6 int main() {
7 string s("This is the zip code of Hodna 1223");
8 cout << "original: " << s <<endl;
9 transform(s.beg in(), s.end(),s.begin (), toupper);
10 cout << "upper: " << s << endl;
11 }

results in the following error output when executing 'c++ t.cpp'

t.cpp: In function ‘int main()’:
t.cpp:9: error: no matching function for call
to ‘transform(__ gnu_cxx::__norm al_iterator<cha r*, std::basic_stri ng<char,
std::char_trait s<char>, std::allocator< char >,
__gnu_cxx::__no rmal_iterator<c har*, std::basic_stri ng<char,
std::char_trait s<char>, std::allocator< char >,
__gnu_cxx::__no rmal_iterator<c har*, std::basic_stri ng<char,
std::char_trait s<char>, std::allocator< char >, <unknown type>)’

Something seems seriously wrong but I can't figure it.

Help, suggestions greatly appreciated.
Jan 6 '08
11 2995
On Jan 7, 4:03 pm, "Gerald I. Evenden" <gerald.even... @verizon.mail>
wrote:
Digging through Prada's "C++ Primer Plus" (5th) I found notes
on p.936-7 relating to C library functions delared as having
int returns and problems thereof, so I added a line to t.cpp
and changed line 13's function name:

1 #include <string>
2 #include <iostream>
3 #include <algorithm>
4 #include <cctype>
5 using namespace std;
6 /* I added the following line because cctype
7 * apparently declares 'int toupper(char)'
8 */
It had better be: "int std::toupper( int )".
9 static char toUpper(char c) { return toupper(c); }
return toupper( static_cast< unsigned char >( c ) ) ;

It's undefined behavior to call the std::toupper( int ) above
with any negative value other than EOF, and char is often
signed.
10 int main() {
11 string s("This is the zip code of Hodna 1223");
12 cout << "original: " << s <<endl;
13 transform(s.beg in(), s.end(),s.begin (), toUpper);
14 cout << "upper: " << s << endl;
15 }
then:
gie@charon:~/Letters/src$ g++ t.cpp
gie@charon:~/Letters/src$ ./a.out
original: This is the zip code of Hodna 1223
upper: THIS IS THE ZIP CODE OF HODNA 1223
gie@charon:~/Letters/src$
Problem solved.
If all the code has to handle is this one string, then there are
much, much easier ways to do it. In general, the above code has
undefined behavior.

And of course, the reason why the code now compiles is because
there is only one isUpper function, so overload resolution has
no problem choosing.

--
James Kanze (GABI Software) mailto:ja****** ***@gmail.com
Conseils en informatique orient�e objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34
Jan 7 '08 #11
James Kanze wrote:
On Jan 7, 4:03 pm, "Gerald I. Evenden" <gerald.even... @verizon.mail>
wrote:
>Digging through Prada's "C++ Primer Plus" (5th) I found notes
on p.936-7 relating to C library functions delared as having
int returns and problems thereof, so I added a line to t.cpp
and changed line 13's function name:

1 #include <string>
2 #include <iostream>
3 #include <algorithm>
4 #include <cctype>
5 using namespace std;
6 /* I added the following line because cctype
7 * apparently declares 'int toupper(char)'
8 */

It had better be: "int std::toupper( int )".
Yes:

static int toUpper(int c) { return toupper(c); }

Works. This seems to negate Prada's argument about type problems.
Also
transform(s.beg in(), s.end(),s.begin (), ::toupper);
seems to work (also dropping the toUpper line):

Alas!! Too much Greek. Note "std::toupp er" does NOT work.
> 9 static char toUpper(char c) { return toupper(c); }

return toupper( static_cast< unsigned char >( c ) ) ;

It's undefined behavior to call the std::toupper( int ) above
with any negative value other than EOF, and char is often
signed.
> 10 int main() {
11 string s("This is the zip code of Hodna 1223");
12 cout << "original: " << s <<endl;
13 transform(s.beg in(), s.end(),s.begin (), toUpper);
14 cout << "upper: " << s << endl;
15 }
>then:
gie@charon:~/Letters/src$ g++ t.cpp
gie@charon:~/Letters/src$ ./a.out
original: This is the zip code of Hodna 1223
upper: THIS IS THE ZIP CODE OF HODNA 1223
gie@charon:~/Letters/src$
Problem solved.

If all the code has to handle is this one string, then there are
much, much easier ways to do it. In general, the above code has
undefined behavior.
Outside of resorting to C char types what would be the efficient
alternative? The above looks more nifty than what I would have
to do in straight C.
And of course, the reason why the code now compiles is because
there is only one isUpper function, so overload resolution has
no problem choosing.
First, I am a total C++ dumbkopf and only an old time C user that
likes to take advantages of some of C++ notational short cuts.
In this case I do not see where the overloading of toupper is coming
from. From what you say it seems to imply that there are multiple
declarations of toupper---where?? I have only seen toupper used
in this basic example and each referred to cctype which I assumed
contained the *only* definition.

Thanks for your comments. They sparked me to look a little further.
--
James Kanze (GABI Software) mailto:ja****** ***@gmail.com
Conseils en informatique orient�e objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34
Jan 7 '08 #12

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

Similar topics

20
3855
by: Steffen Brinkmann | last post by:
Hi! I tried to modify the transform algorithm in a way that it doesn't take iterators, but a reference to a container class and a value, because Mostly I need to do an operation of a container and a single number (e.g. multiply all the values in a vector by 3 or so). So, this is my intent: #ifndef ALGORITHM_EXT_HH #define ALGORITHM_EXT_HH
5
4325
by: google | last post by:
Hi All, I'm just getting started learning to use <algorithm> instead of loads of little for loops, and I'm looking for a bit of advice/mentoring re: implementing the following... I have a vector of boost::shared_ptr's, some of which may be NULL. I'd like to find out if there are any non-NULL elements in the vector. Here's what I have at the moment.
7
2609
by: Wei | last post by:
Hi all, I found out I can use the max function which is defined in STL <algorithmwithout including this header in my program. The compilers I used are GNU g++ 3.4.4 and Visual Studio C++ 2005. The program is as follows: #include <iostream> //#include <algorithm <-- I don't have to include <algorithmin
10
6082
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to remove the elements with odd values from your list * and the even values from your vector.
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9602
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,...
1
10383
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
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5550
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...
0
5688
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
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
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.