string字符串的比较
代码语言:javascript复制#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;
}
第二种比较法:
代码语言:javascript复制#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;
}