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;
}
複制