string
对象的比较
1.直接使用==
,相等返回1,不相等返回0;
2.使用string对象的compare方法,相等返回0,str1>str2返回1,str1<str返回-1;
例子:
#include "string.h"
#include "iostream.h"
using namespace std;
int main(){
string str1 = "aaa";
string str2 = "aaa";
string str3 = "bbb";
cout << "======" << endl;
cout << "str1 = " << str1 << endl;
cout << "str2 = " << str2 << endl;
cout << "str3 = " << str3 << endl;
cout << "str1 == str2 : " << (str1==str2) << endl;
cout << "str1 == str3 : " << (str1==str3) << endl;
cout << "str3 == str1 : " << (str3==str1) << endl;
cout << "str1 compare str2 : " << (str1.compare(str2)) << endl;
cout << "str1 compare str3 : " << (str1.compare(str3)) << endl;
cout << "str3 compare str1 : " << (str3.compare(str1)) << endl;
return 0;
}
输出: