VS2012

hywxn1 2014-04-06 04:16:38
C++primer第五版上面有一个题是从cin读入一组词并把它们存入vector对象,然后都改写成大写形式,程序段如下,用的是VS2012,可是编译错误:error C2664: “toupper”: 不能将参数 1 从“std::basic_string<_Elem,_Traits,_Alloc>”转换为“int”
这是怎么回事啊。
vector<string> v;
string word;
while(cin>>word)
{
v.push_back(word);
}
for(auto &c:v)
c=toupper(c);
...全文
164 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zybjtu 2014-04-08
  • 打赏
  • 举报
回复
for(auto &c:v) // 这个语句是遍历vector v里面的string c=toupper(c); // 这个语句是要把string 转换为大写。但是,toupper接受的是字符,不接受string。 你应该一个一个转化为大写
for(auto &str : v){
      for(auto &ch : str){
             char tmp = toupper(ch);
             cout << tmp << endl;
      }
}
赵4老师 2014-04-08
  • 打赏
  • 举报
回复
toupper _toupper, towupper Convert character to uppercase. int toupper( int c ); int _toupper( int c ); int towupper( wint_t c ); Routine Required Header Compatibility toupper <stdlib.h> and <ctype.h> ANSI, Win 95, Win NT _toupper <ctype.h> Win 95, Win NT towupper <ctype.h> or <wchar.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these routines converts a copy of c, if possible, and returns the result. If c is a wide character for which iswlower is true and there is a corresponding wide character for which iswupper is true, towupper returns the corresponding wide character; otherwise, towupper returns c unchanged. There is no return value reserved to indicate an error. Parameter c Character to convert Remarks Each of these routines converts a given lowercase letter to an uppercase letter if possible and appropriate. Data Conversion Routines See Also is Routines, to Functions Overview Example /* TOUPPER.C: This program uses toupper and tolower to * analyze all characters between 0x0 and 0x7F. It also * applies _toupper and _tolower to any code in this * range for which these functions make sense. */ #include <conio.h> #include <ctype.h> #include <string.h> char msg[] = "Some of THESE letters are Capitals\r\n"; char *p; void main( void ) { _cputs( msg ); /* Reverse case of message. */ for( p = msg; p < msg + strlen( msg ); p++ ) { if( islower( *p ) ) _putch( _toupper( *p ) ); else if( isupper( *p ) ) _putch( _tolower( *p ) ); else _putch( *p ); } } Output Some of THESE letters are Capitals sOME OF these LETTERS ARE cAPITALS
hywxn1 2014-04-08
  • 打赏
  • 举报
回复
引用 10 楼 zyaiwx 的回复:
for(auto &c:v) // 这个语句是遍历vector v里面的string c=toupper(c); // 这个语句是要把string 转换为大写。但是,toupper接受的是字符,不接受string。 你应该一个一个转化为大写
for(auto &str : v){
      for(auto &ch : str){
             char tmp = toupper(ch);
             cout << tmp << endl;
      }
}
冥思苦想了两天终于弄清楚了,就是这个意思!
MRJUN00 2014-04-07
  • 打赏
  • 举报
回复
命名空间与头文件都加齐了吗,这书3.2节有说明后面例子都省掉了这些
buyong 2014-04-07
  • 打赏
  • 举报
回复
应该是vs2012的问题,换2013试试,或者换g++
hywxn1 2014-04-07
  • 打赏
  • 举报
回复
引用 6 楼 buyong 的回复:
应该是vs2012的问题,换2013试试,或者换g++
g++多少版本,在windows上运行会不会有bug, 在网上没有下到啊
hywxn1 2014-04-07
  • 打赏
  • 举报
回复
引用 5 楼 MRJUN00 的回复:
命名空间与头文件都加齐了吗,这书3.2节有说明后面例子都省掉了这些
都加了
hywxn1 2014-04-06
  • 打赏
  • 举报
回复
书上不是写的如果C是小写字母,toupper(c)就是输出大写字母吗,没有说要求c是int类型啊
-LanPei- 2014-04-06
  • 打赏
  • 举报
回复
VS2012不完全支持C++11,C++Primer第5版覆盖了C++11的内容。所以建议还是用g++4.8以上的版本进行编译。
  • 打赏
  • 举报
回复
得先了解每个变量都什么类型的 c是std::string类型,toupper参数要求int类型,自然无法通过编译了。
hywxn1 2014-04-06
  • 打赏
  • 举报
回复
而且,比如定义了一个:vector<string> v; auto &c=v; cout<<c;这样会提示输出符号<<有错误; 如果是 for(auto &c:v) cout<<c;这样就没错误; 而且直接用cout<<v;更是提示输出符号<<有错误; 为什么为什么为什么啊,头都大了

65,208

社区成员

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

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