C++中 string对象的大小比较
原理:两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇’\0’为止。当两个数的位数一样,则直接可以应用字符串的比较。如 "1346" > "1111" == true例子:#include<iostream>#include<string>using namespace std;int main(){string str1("235");
文章共274字 · 阅读需要大约1分钟
一键AI生成摘要,助你高效阅读
问答
·
原理:
两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇’\0’为止。
当两个数的位数一样,则直接可以应用字符串的比较。如
"1346" > "1111" == true
例子:
#include<iostream>
#include<string>
using namespace std;
int main(){
string str1("235");
string str2("121");
bool result;
result = str1 > str2;
cout<<result<<endl; // 1
str1 = "1111";
result = str1 > str2;
cout<<result<<endl; // 0
str1 = "111";
result = str1 > str2;
cout<<result<<endl; // 0
return 0;
}
更多推荐
已为社区贡献2条内容
所有评论(0)