难道又是缓冲区问题

starcat 2010-04-03 11:06:24
书上例子:
#include <string>
#include <iostream>
using namespace std;
int main() {
string first("This");
string second("That");
// Which is lexically greater?
switch(first.compare(second)) {
case 0: // The same
cout << first << " and " << second <<
" are lexically equal" << endl;
break;
case -1: // Less than
first.swap(second);
// Fall through this case...
case 1: // Greater than
cout << first <<
" is lexically greater than " <<
second << endl;
}
}
结果,没有输出……
...全文
102 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
starcat 2010-04-04
  • 打赏
  • 举报
回复
原因找到了,我这儿compare()的返回值是8,看来是我人品不好了
#include <string>
#include <iostream>
using namespace std;
int main() {
string first("This");
string second("That");
int i;
// Which is lexically greater?
switch(i = first.compare(second)) {
case 0: // The same
cout << first << " and " << second <<
" are lexically equal" << endl;
break;
case -1: // Less than
first.swap(second);
// Fall through this case...
case 1: // Greater than
cout << first <<
" is lexically greater than " <<
second << endl;
}
cout << "i = " << i << endl;
}
scudc 2010-04-04
  • 打赏
  • 举报
回复
vc6.0可以输出。。。。。。。。。。。。
buptzwp 2010-04-04
  • 打赏
  • 举报
回复
g++下没问题呀,compare()怎么会返回8呀?
yuzl32 2010-04-04
  • 打赏
  • 举报
回复
'i' - 'a' = 8
lovesi3344 2010-04-03
  • 打赏
  • 举报
回复
我加完就输出了2楼的结果
难道不是吗?

[Quote=引用 4 楼 starcat 的回复:]
呃,不是这个问题啊
我用的BCC 5.5命令行编译器,本来就不用加暂停的
[/Quote]
starcat 2010-04-03
  • 打赏
  • 举报
回复
呃,不是这个问题啊
我用的BCC 5.5命令行编译器,本来就不用加暂停的
lovesi3344 2010-04-03
  • 打赏
  • 举报
回复
加上这句
system("pause");//mark
stardust20 2010-04-03
  • 打赏
  • 举报
回复
试了下。。输出This is lexically greater than That
lovesi3344 2010-04-03
  • 打赏
  • 举报
回复
#include <string>
#include <iostream>
using namespace std;
int main()
{
string first("This");
string second("That");
// Which is lexically greater?
switch(first.compare(second))
{
case 0: // The same
cout << first << " and " << second <<" are lexically equal" << endl;break;
case -1: // Less than
first.swap(second);
// Fall through this case...
case 1: // Greater than
cout << first <<" is lexically greater than " <<second << endl;
}
system("pause");//mark
}

64,648

社区成员

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

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