string容器05之string字串的比較
阿新 • • 發佈:2021-02-09
技術標籤:stl學習之string字串c++stl
string字串的比較
#include<iostream>
using namespace std;
//string字串的比較
void test()
{
string s1 = "abc";
string s2 = "abc";
int ret=s1.compare(s2);
if (ret == 0)
{
cout << "s1==s2" << endl;
}
else if (ret > 0)
{
cout << "s1>s2" << endl;
}
else {
cout << "s1<s2" << endl;
}
}
int main()
{
test();
system("pause");
return 0;
}
第二種比較法:
#include<iostream>
using namespace std;
//string字串的比較
void test()
{
string s1 = "abc";
string s2 = "abcd" ;
if (s1==s2)
{
cout << "s1==s2" << endl;
}
else if (s1>s2)
{
cout << "s1>s2" << endl;
}
else {
cout << "s1<s2" << endl;
}
}
int main()
{
test();
system("pause");
return 0;
}