string類可否直接經較大小?

wangyangcheng 2003-10-21 01:47:12
借用一下樓主寶地﹕
string max(string str1,string str2)
{
string str1="abc",str2="cda";
return str1>str2?str1:str2;
}


如上代碼﹐string類可以直接比較大小嗎?
Thanks!
...全文
136 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangyangcheng 2003-10-22
  • 打赏
  • 举报
回复
謝謝各位的回復。
再問一個關于exception的問題:

若我傳入一個非string的實參給string的形參﹐會出現什么情況?我在VC6.0下面它不會報警﹐程序停在哪里﹐也無法結束﹐但也沒死。

誰可談一下這方面的嗎?
wangyangcheng 2003-10-22
  • 打赏
  • 举报
回复
How many vendors of STL?
zoohoo 2003-10-21
  • 打赏
  • 举报
回复
ms的string没有重载小于等符合,其他公司的stl都是可以的。
Himmel 2003-10-21
  • 打赏
  • 举报
回复
可以
Syntax

#include <string.h>
int strcmp(const char *s1, const char *s2);
int _fstrcmp(const far char *s1, const far char *s2);
int wcscmp(const wchar_t *s1, const wchar_t *s2);

#include <mbstring.h>
int _mbscmp(const unsigned char *s1, const unsigned char *s2);

if s1>s2 return >0
if s1==s2 return ==0
if s1<s2 return <0

from Borland c++ 5.02帮助文件
JetGeng 2003-10-21
  • 打赏
  • 举报
回复
可以的。
njuhuangmy 2003-10-21
  • 打赏
  • 举报
回复
可以的阿

#include <iostream>
#include <string>
using namespace std;

int
main ()
{
string s1("abc"), s2("bac");
if (s1 > s2)
cout << " > " << endl;
else
cout << " < " << endl;
return 1;
}

.net vc++ 编译通过,执行 正确
dddd8888 2003-10-21
  • 打赏
  • 举报
回复
因该有个成员函数把
Hot_Forever 2003-10-21
  • 打赏
  • 举报
回复
#include <iostream>
#include <cstdlib>
using namespace std;

string& max(string& str1,string& str2)
{
return str1>str2?str1:str2;
}

int main()
{
string str3="abc";
string str4="cda";
string str5=max(str3,str4);
cout<<str5<<endl;
system("pause");
return 0;
}
DEV-cpp下编译通过,VC6下有错误,只能说明VC6对标准C++的支持不行
wangyangcheng 2003-10-21
  • 打赏
  • 举报
回复
but I can't compile code above mentioned success.

Who can try?

Thanks!
Hot_Forever 2003-10-21
  • 打赏
  • 举报
回复
可以这样比较的,没有问题,string class 提供了==,!=,<,>,<=,>=
并且可以2个string 对象,或者是1个string对象以及1个C字符串的比较

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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