473,378 Members | 1,482 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

transform algorithm and casting of functions

Hi

I saw this code in the net for converting case.

string s = "Amma";
transform(s.begin(),s.end(),s.begin(),(int(*)(int) )toupper);

Q: what is the meaning of (int(*)(int))toupper ?

The code works fine when (int(*)(int))toupper is replaced
by ::toupper. But the code does not even compile, when i write just
std::toupper i.e.
transform(s.begin(),s.end(),s.begin(),std::toupper ); //not even
compiling

Could you please explain the difference between the three?

thanks
suresh

Dec 22 '07 #1
2 1685
On Dec 22, 8:08*pm, suresh <suresh.amritap...@gmail.comwrote:
Hi

I saw this code in the net for converting case.

string s = "Amma";
transform(s.begin(),s.end(),s.begin(),(int(*)(int) )toupper);

Q: what is the meaning of *(int(*)(int))toupper ?
It means it is casting pointer to function toupper as a pointer to a
function having return type as int and takes one int argument. I think
that is not required as toupper is of the same function pointer type.
The code works fine when (int(*)(int))toupper is replaced
by ::toupper. But the code does not even compile, when i write just
std::toupper i.e.
transform(s.begin(),s.end(),s.begin(),std::toupper ); //not even
compiling

Could you please explain the difference between the three?
I think you are using VC++ 6.0 (or something as old/non-conforming as
that). It does not put the C APIs into std namespace (they are in
global namespace). Get rid of it and std::toupper works.
Dec 22 '07 #2
Thanks abhishek for the comments. But I am using g++ on a debian etch
machine. It is not VC++ and std::toupper is not compiling...

any suggestions?

suresh

On Dec 22, 10:34 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
On Dec 22, 8:08 pm,suresh<suresh.amritap...@gmail.comwrote:
Hi
I saw this code in the net for converting case.
string s = "Amma";
transform(s.begin(),s.end(),s.begin(),(int(*)(int) )toupper);
Q: what is the meaning of (int(*)(int))toupper ?

It means it is casting pointer to function toupper as a pointer to a
function having return type as int and takes one int argument. I think
that is not required as toupper is of the same function pointer type.
The code works fine when (int(*)(int))toupper is replaced
by ::toupper. But the code does not even compile, when i write just
std::toupper i.e.
transform(s.begin(),s.end(),s.begin(),std::toupper ); //not even
compiling
Could you please explain the difference between the three?

I think you are using VC++ 6.0 (or something as old/non-conforming as
that). It does not put the C APIs into std namespace (they are in
global namespace). Get rid of it and std::toupper works.
Dec 30 '07 #3

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

Similar topics

20
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...
5
by: Alex Vinokur | last post by:
Functor-parameters in the for_each() and transform() algorithms are passed by value. Might it make sense to have also algorithms for_each2() and transform2() which pass Functor-parameters by...
0
by: Alex Vinokur | last post by:
One more improvement has been done in the "Computing very large Fibonacci numbers" algorithm. Its new version can be seen at http://groups.google.com/groups?selm=2mk62rFo1mv0U1%40uni-berlin.de ...
1
by: Brian McGuinness | last post by:
I have a question about using the STL transform algorithm in a function. What I want to do is define a group of array classes to represent APL-style arrays (arrays in which the number of...
9
by: Saki Arkoudopoulos | last post by:
Hello, I'm trying to convert a string with mixed upper- and lowercase characters to all lowercase characters. I found the following code on the Internet: transform(temp.begin(), temp.end(),...
9
by: Patrick Guio | last post by:
Dear all, I am trying to use the std::transform algorithm to to the following vector< vector<char> >::iterator ik = keys.begin(); // key list iterator vector< vector<char> >::iterator is = ik;...
4
by: Ney André de Mello Zunino | last post by:
Hello. The following program: #include <list> #include <iterator> #include <algorithm> #include <cmath> #include <iostream>
11
by: Gerald I. Evenden | last post by:
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>...
19
by: Taras_96 | last post by:
Hi all, A poster at http://bytes.com/forum/thread60652.html implies that using strtoupper in transform doesn't work because ctype.h may define strtoupper as a macro: "The problem is that most...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.