08 string容器 - 字符串比较

功能描述:

  • 字符串之间的比较

比较方式:

  • 字符串比较是按字符的ASCII码进行对比

= 返回 0

> 返回 1

< 返回 -1

函数原型:

int compare(const string &s) const; //与字符串s比较

int compare(const char *s) const; //与字符串s比较

cpp 复制代码
#include<iostream>
using namespace std;

void test01() {
	string str1 = "hello";
	string str2 = "hello";

	if (str1.compare(str2) == 0) {
		cout << "str1 = str2" << endl;
	}
	else if (str1.compare(str2) > 0) {
		cout << "str1 > str2" << endl;
	}
	else{
		cout << "str1 < str2" << endl;
	}
}


int main() {
	test01();

	system("pause");
	return 0;
}

总结:字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义并不是很大,算法题里可能用到

相关推荐
进击的小头2 小时前
移动平均滤波器:从原理到DSP ADC采样实战(C语言实现)
c语言·开发语言·算法
漂洋过海的鱼儿2 小时前
Qt--元对象系统
开发语言·数据库·qt
cyforkk2 小时前
05、Java 基础硬核复习:数组的本质与面试考点
java·开发语言·面试
csbysj20202 小时前
jQuery Growl:实现优雅的通知效果
开发语言
历程里程碑2 小时前
双指针--双数之和
开发语言·数据结构·c++·算法·排序算法·哈希算法·散列表
txwtech2 小时前
第24篇 vs2019QT QChart* chart = new QChart()发生访问冲突
开发语言·qt
Physicist in Geophy.2 小时前
b-value explain by first principle
开发语言·php
雨季6662 小时前
构建 OpenHarmony 简易密码强度指示器:用字符串长度实现直观反馈
android·开发语言·javascript
橘橙黄又青2 小时前
List和Map篇
java·开发语言·面试