string容器05之string字符串的比较

2021-03-02 16:57:35 浏览数 (1)

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

一般用于判段英文字母是否相等

0 人点赞