c ++中stl
String class in C++ is a container used to manage strings and it has many advantages than ordinary c-string. In many programming problems, we require to compare two strings, in this article we discuss library function used to compare two string class objects in C++.
C ++中的字符串类是用于管理字符串的容器,它比普通的c字符串具有许多优点。 在许多编程问题中,我们需要比较两个字符串 ,在本文中,我们讨论用于比较C ++中两个字符串类对象的库函数。
We use std::string::compare() function to comparing two string objects. It returns '0' if both the strings are equal, integer < 0 if the compared string is shorter and an integer > 0 if the compared string is longer. We can use std::string::compare() function in various forms according to need, some methods are discussed below.
我们使用std :: string :: compare()函数比较两个字符串对象。 如果两个字符串相等,则返回“ 0”;如果比较的字符串较短,则返回整数<0;如果比较的字符串较长,则返回整数> 0。 我们可以根据需要以各种形式使用std :: string :: compare()函数 ,下面讨论一些方法。
References: std::string::compare()
引用: std :: string :: compare()
Example 1:
范例1:
Syntax:
句法:
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;
}
Output
输出量
includehelp didn't match to include
includehelp is equal to includehelp
Example 2:
范例2:
compare (size_t pos, size_t len, const string& str) const;
Here, pos is the index of string from which you want to compare and len denotes the length of the compared string after pos. following code shows the working of this syntax :
在这里, pos是要从中进行比较的字符串的索引, len表示在pos之后要比较的字符串的长度。 以下代码显示了此语法的工作方式:
#include<iostream>
using namespace std;
int main()
{
string s1("help");
string s2("includehelp");
//Compares 4 characters from index 7 of s2 with s1
if((s2.compare(7, 4, s1)) == 0)
cout << s1 << " is equal to " << s2.substr(7) << endl;
else
cout << s1 << " didn't match to " << s2.substr(7) << endl;
return 0;
}
Output
输出量
help is equal to help
Example 3:
范例3:
It compares len characters from index number pos of first string with sublen characters from index subpos of string compared string i.e str. following code shows the working of this syntax :
它比较第一个字符串的索引编号pos中的len个字符与比较字符串的索引subpos中的sublen个字符,即str 。 以下代码显示了此语法的工作方式:
#include<iostream>
using namespace std;
int main()
{
string s1("includenothelp");
string s2("helpinclude");
cout<<"s1 = "<<s1<<endl;
cout<<"s2 = "<<s2<<endl;
cout<<"Compares 7 characters from index 0 of s1 with 7 characters from index 4 of s2 "<<endl;
// Compares 7 characters from index 0 of s1 with 7 characters from index 4 of s2
if((s1.compare(0, 7, s2, 4, 7)) == 0)
cout << s1.substr(0,7) <<" of "<< s1 << " is equal to " << s2.substr(4) <<" of "<<s2 << endl;
else
cout <<" didn't match "<< endl;
return 0;
}
Output
输出量
Compares 7 characters from index 0 of s1 with 7 characters from index 4 of s2
include of includenothelp is equal to include of helpinclude
翻译自: https://www.includehelp.com/stl/compare-two-string-objects.aspx
c ++中stl