[转]C++中 string对象的大小比较

  转自:

    https://blog.csdn.net/jason_cuijiahui/article/details/79038468

 

原理:
两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇’\0’为止。

当两个数的位数一样,则直接可以应用字符串的比较。如

"1346" > "1111" == true
1
例子:

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 
 5 int main(){
 6 string str1("235");
 7 string str2("121");
 8 
 9 bool result;
10 result = str1 > str2;
11 cout<<result<<endl; // 1
12 
13 str1 = "1111";
14 result = str1 > str2;
15 cout<<result<<endl; // 0
16 
17 str1 = "111";
18 result = str1 > str2;
19 cout<<result<<endl; // 0
20 
21 return 0;
22 }

 

posted @ 2019-10-15 19:25  博客园—哆啦A梦  阅读(694)  评论(0)    收藏  举报