string中的compare() 2011.08.09

来源:互联网 发布:windows to go配置 编辑:程序博客网 时间:2024/06/06 10:57
#include <iostream>#include <string>using namespace std;int main(){string str = "hello";cout<<str.compare("hello")<<endl;cout<<str.compare("hello world")<<endl;cout<<str.compare("ill")<<endl;cout<<str.compare("glad")<<endl;return 0;}

输出:

0

-1

-1

1

compar()函数是用来比较字符串的,如果两个字符串相同返回0

如果两个字符串不同,返回非0:

当第一个字符串str大于第二个字符串的时候,返回1

当第一个字符串str小于第二个字符串的时候,返回-1

两个字符串完全不同的时候,其实就是比较的两个字符串的第一个字母的ascii值


刚才QQ群里有网友问,觉得我也该复习一下了,就去看了一眼,回忆了一下。


原创粉丝点击