函数与函数对象.

du51 2006-07-07 09:46:16
经常见到网上有.如下的语句:
string s("heLLo");
transform(s.begin(), s.end(), s.begin(), toupper);

toupper是C的函数
但是,我从来没有编译通过.怎样才能通过?
...全文
184 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
du51 2006-07-07
  • 打赏
  • 举报
回复
UPCC实在不好意思..
我结贴子的时候,还没见到你回..
以后分再补上...不好意思呀..
Dong 2006-07-07
  • 打赏
  • 举报
回复
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

typedef int (*fun)(int);
void test(char* s,fun f)
{
while(*s != '\0')
{
putchar(f(*s));
s++;
}
}

main()
{
char str[10];
strcpy(str,"shENg");
test(str,toupper);
printf("\n");
test(str,tolower);
getchar();
}
du51 2006-07-07
  • 打赏
  • 举报
回复
谢谢楼上了.明白了..
我试了好几次,没带参数..
转binary_function又转不了.

谢谢了.
sinall 2006-07-07
  • 打赏
  • 举报
回复
可能是:
v2401% man -all toupper
toupper (3c++) -M /opt/SUNWspro/man
toupper (3c) -M /usr/man

两个toupper冲突,如果选择c++风格的toupper,则需要ptr_fun<int, int>(&toupper)进行实例化。
sinall 2006-07-07
  • 打赏
  • 举报
回复
include <iostream>
#include <string>
#include <algorithm>
#include <locale>
#include <functional>
using namespace std;

int main(void)
{
string s("heLLo");
transform(s.begin(), s.end(), s.begin(), ptr_fun<int, int>(&toupper));
cout << s << endl;
return 0;
}
或者:
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;

int main(void)
{
string s("heLLo");
transform(s.begin(), s.end(), s.begin(), ::toupper);
cout << s << endl;
return 0;
}
du51 2006-07-07
  • 打赏
  • 举报
回复
还有一楼的..
STL的国际化那部分我还没看到.不敢乱说..
但是那个程序,在DEV-CPP下运行挂了.
是和系统有关吗?
du51 2006-07-07
  • 打赏
  • 举报
回复
把你文件贴出来.
foochow 2006-07-07
  • 打赏
  • 举报
回复
可以通过啊
PMsg 2006-07-07
  • 打赏
  • 举报
回复
Standard C++ Library Reference

toupperSee Also
<locale> Members
Converts a character to upper case.

template<Class CharType>
CharType toupper(
CharType _Ch,
const locale& _Loc
) const;
Parameters
_Ch
The character to be converted to upper case.
_Loc
The locale containing the character to be converted.
Return Value
The character converted to upper case.

Remarks
The template function returns use_facet<ctype<CharType> >(_Loc).toupper(_Ch).

Example
// locale_toupper.cpp
// compile with: /EHsc
#include <locale>
#include <iostream>
using namespace std;

int main( )
{
locale loc ( "German_Germany" );
char result1 = toupper ( 'h', loc );
cout << "The upper case of 'h' in the locale is: "
<< result1 << "." << endl;
char result2 = toupper ( 'H', loc );
cout << "The upper case of 'H' in the locale is: "
<< result2 << "." << endl;
char result3 = toupper ( '$', loc );
cout << "The upper case of '$' in the locale is: "
<< result3 << "." << endl;
}
Output
The upper case of 'h' in the locale is: H.
The upper case of 'H' in the locale is: H.
The upper case of '$' in the locale is: $.
See Also
<locale> Members



--------------------------------------------------------------------------------

Send feedback on this topic to Microsoft

© 1992-2002 by P. J. Plauger. All rights reserved.

© Microsoft Corporation. All rights reserved.

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧