比较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 入住酒店文案祝福语简短