string类的compare输出令我昏迷不醒

najiushifeng2 2007-07-31 11:11:37
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1="5130";
string s2="52";
char c[]="34";
int i,j,k,l,m,n;
i=s1.compare(s2);//将s2与s1比较,返回0为相等,1为s1大于s2,-1为s1小于s2
j=s2.compare(c);//将char型字符串c与s2比较
k=s1.compare(0,2,s2);//取s1前两个字符与s1做比较,参数1表示取的s1的字符长度
l=s1.compare(1,1,s2,0,1);//取s1[1]与s2[0]做比较,参数2和4分别表示取的s1和s2的字符长度
m=s1.compare(1,1,c,0,1);//取s1[1]与c[0]做比较,参数2和4分别表示取的s1和c的字符长度
n=s1.compare(1,1,c,1);//取s1[1]与c[0]做比较,参数1表示取的s1的字符长度
cout<<s1<<":"<<s2<<"="<<i<<endl; //依次输出各次比较
cout<<s2<<":"<<c<<"="<<j<<endl;
cout<<s1[0]<<s1[1]<<":"<<s2<<"="<<k<<endl;
cout<<s1[1]<<":"<<s2[0]<<"="<<l<<endl;
cout<<s1[1]<<":"<<c[0]<<"="<<m<<endl;
cout<<s1[1]<<":"<<c[0]<<"="<<n<<endl;
return 0;
}
compare比较字符串究竟以什么为基准,是一个字符一个字符的比较完然后汇总出所有比较结果,还是只比较第一字符就判断两个字符串的大小呢?糊涂!!另外string类的实现部分在哪里能看到,不要定义。
...全文
378 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
najiushifeng2 2007-08-01
  • 打赏
  • 举报
回复
?????
iambic 2007-07-31
  • 打赏
  • 举报
回复
lexical compare
everysmile 2007-07-31
  • 打赏
  • 举报
回复
好像在MSDN里面有讲。还蛮详细的
najiushifeng2 2007-07-31
  • 打赏
  • 举报
回复
请问大家,瞌睡虫说的对不对,对的话我就直接结贴了。
najiushifeng2 2007-07-31
  • 打赏
  • 举报
回复
谢谢瞌睡虫
jixingzhong 2007-07-31
  • 打赏
  • 举报
回复
compare比较字符串究竟以什么为基准,是一个字符一个字符的比较完然后汇总出所有比较结果,还是只比较第一字符就判断两个字符串的大小呢?
========================
依次比较,
当当前字符相等时候,比较下一个字符。

如果当前字符可以知道大小,
则后面字符不再比较。

比较依据是ASCII码值大小。
jixingzhong 2007-07-31
  • 打赏
  • 举报
回复
另外string类的实现部分在哪里能看到,不要定义。

看STL源码 ...
jixingzhong 2007-07-31
  • 打赏
  • 举报
回复
www.cppreference.com/cppstring/compare.html
jixingzhong 2007-07-31
  • 打赏
  • 举报
回复
compare
Syntax:
#include <string>
int compare( const string& str );
int compare( const char* str );
int compare( size_type index, size_type length, const string& str );
int compare( size_type index, size_type length, const string& str, size_type index2,
size_type length2 );
int compare( size_type index, size_type length, const char* str, size_type length2 );

The compare() function either compares str to the current string in a variety of ways, returning

Return Value Case
less than zero this < str
zero this == str
greater than zero this > str

The various functions either:

compare str to the current string,
compare str to a substring of the current string, starting at index for length characters,
compare a substring of str to a substring of the current string, where index2 and length2 refer to str and index and length refer to the current string,
or compare a substring of str to a substring of the current string, where the substring of str begins at zero and is length2 characters long, and the substring of the current string begins at index and is length characters long.
For example, the following code uses compare() to compare four strings with eachother:

string names[] = {"Homer", "Marge", "3-eyed fish", "inanimate carbon rod"};

for( int i = 0; i < 4; i++ ) {
for( int j = 0; j < 4; j++ ) {
cout << names[i].compare( names[j] ) << " ";
}
cout << endl;
}

Data from the above code was used to generate this table, which shows how the various strings compare to eachother:

Homer Marge 3-eyed fish inanimate carbon rod
"Homer".compare( x ) 0 -1 1 -1
"Marge".compare( x ) 1 0 1 -1
"3-eyed fish".compare( x ) -1 -1 0 -1
"inanimate carbon rod".compare( x ) 1 1 1 0
taodm 2007-07-31
  • 打赏
  • 举报
回复
和strcmp相同。
当然在string这个头文件里,它也许会再#include一个真正实现文件,比如basic_string
因为string是basic_string模板类的一个特化版本。
najiushifeng2 2007-07-31
  • 打赏
  • 举报
回复
当当前字符相等时候,比较下一个字符。

如果当前字符可以知道大小,
则后面字符不再比较。

比较依据是ASCII码值大小。
好象输出的结果不太对
najiushifeng2 2007-07-31
  • 打赏
  • 举报
回复
请问大家,瞌睡虫说的对不对,对的话我就直接结贴了。

65,209

社区成员

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

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