在C ++中使用关系运算符比较字符串对象
在这里,我们将看到如何在C++中比较两个字符串。C++具有字符串类。它compare()在标准库中还具有比较字符串的功能。但是这里我们将使用条件运算符,例如==,!=,<,>,<=,>=。这些运算符逐个字符地检查字符串。让我们看一下代码以获得更好的主意。
示例
#include<iostream>
using namespace std;
void compareStrings(string s1, string s2) {
if (s1 != s2)
cout << s1 << " is not equal to "<< s2 << endl;
else if(s1 == s2)
cout << "Strings are equal";
if (s1 > s2)
cout << s1 << " is greater than "<< s2 << endl;
else if(s1 < s2)
cout << s2 << " is greater than "<< s1 << endl;
}
int main() {
string s1("hello");
string s2("helLo");
compareStrings(s1, s2);
}输出结果
hello is not equal to helLo hello is greater than helLo
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短