比较C ++中的两个字符串对象 C ++ STL
C++中的字符串类是用于管理字符串的容器,它比普通的c字符串具有许多优点。在许多编程问题中,我们需要比较两个字符串,在本文中,我们讨论用于比较C++中两个字符串类对象的库函数。
我们使用std::string::compare()函数比较两个字符串对象。如果两个字符串相等,则返回“0”;如果比较的字符串较短,则返回整数<0;如果比较的字符串较长,则返回整数>0。我们可以根据需要以各种形式使用std::string::compare()函数,下面讨论一些方法。
引用:std::string::compare()
范例1:
语法:
string::compare (const string& str) const
#include<iostream>
using namespace std;
int main() {
string s1("includehelp");
string s2("include");
string s3("includehelp");
if((s1.compare(s2)) == 0)
cout << s1 << " is equal to " << s2 << endl;
else
cout << s1 << " didn't match to " << s2 << endl;
if((s1.compare(s3)) == 0)
cout << s1 << " is equal to " << s3 << endl;
else
cout << s1 << " didn't match to " << s3 << endl;
return 0;
}输出结果
includehelp didn't match to include includehelp is equal to includehelp
热门推荐
10 家长毕业祝福语简短 励志
11 简短他人的生日祝福语
12 写给宝宝新年祝福语简短
13 亲人提车祝福语简短
14 洁字谐音祝福语简短
15 靳年祝福语大全简短
16 烟酒店开业祝福语简短
17 简短结婚的祝福语大全
18 关于孩子毕业祝福语简短