请教关于 c++ string的 transform()函数

zhen_4321 2008-12-07 01:56:33
#include<iostream>
#include<cctype>
#include<string>
#include<iterator>
using namespace std;
int main()
{
string s("ni hao");
cout<<s<<endl;
transform(s.begin(),s.end(),s.begin(),toupper);
cout<<s<<endl;
}

../src/stringDemo.cpp:113: 错误:对 ‘transform(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unresolved overloaded function type>)’ 的调用没有匹配的函数
...全文
1421 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
bfhtian 2009-02-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 freeflies 的回复:]
#include <iostream>
#include <cctype>
#include <string>
#include <algorithm> //
using namespace std;
int main()
{
string s("ni hao");
cout < <s < <endl;
transform(s.begin(),s.end(),s.begin(),::toupper);
cout < <s < <endl;
}

这样更简单!
[/Quote]
头文件换一下
ffffffflie 2009-02-12
  • 打赏
  • 举报
回复
#include <iostream>
#include <cctype>
#include <string>
#include <iterator>
using namespace std;
int main()
{
string s("ni hao");
cout < <s < <endl;
transform(s.begin(),s.end(),s.begin(),::toupper);
cout < <s < <endl;
}

这样更简单!!
airtrack 2008-12-07
  • 打赏
  • 举报
回复
把程序改成这样,gcc下就不会报错了。

#include <iostream>
#include <cctype>
#include <string>
#include <iterator>
#include <algorithm>
// using namespace std;

int main()
{
std::string s("ni hao");
std::cout << s <<std::endl;
std::transform(s.begin(), s.end(), s.begin(), toupper);
std::cout << s <<std::endl;
return 0;
}
airtrack 2008-12-07
  • 打赏
  • 举报
回复
http://www.megasolutions.net/cplus/GCC-MSVC++-difference-32501.aspx
这个可能解释了原因,其中有一段话:

Right. The problem you face is that VC++ conforms better to the C++
Standard than does GCC. The former provides the required overloads
for exp; the latter is at the mercy of the underlying C library to
provide those overloads, and certainly glibc fails to do so. By
interposing the wrapper function, you disambiguate the call to
transform.
HTH,
P.J. Plauger
Dinkumware, Ltd.


airtrack 2008-12-07
  • 打赏
  • 举报
回复
这个程序先包含#include <algorithm>之后在vc2008下没错,在gcc下确实会报像楼主的一样的错误。
huangyu20072010 2008-12-07
  • 打赏
  • 举报
回复
cout后面不要用< <,改成<<
其他就没看到什么错了,在我的编译器(vs2005)里面也能通过啊
另外,transform可不是string的函数,那个是STL里面的。
airtrack 2008-12-07
  • 打赏
  • 举报
回复
#include <algorithm>

65,187

社区成员

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

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