1. 程式人生 > 其它 >string容器05之string字串的比較

string容器05之string字串的比較

技術標籤: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; }

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