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

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

相关推荐
python猿几秒前
打卡Python王者归来--第30天
开发语言·python
qq_334903151 分钟前
嵌入式C++驱动开发
开发语言·c++·算法
阿贵---12 分钟前
C++代码规范化工具
开发语言·c++·算法
暮冬-  Gentle°20 分钟前
自定义内存检测工具
开发语言·c++·算法
一直都在57220 分钟前
Java死锁
java·开发语言
娇娇yyyyyy23 分钟前
QT编程(15): Qt 按键事件和定时器事件
开发语言·qt
2501_9454248038 分钟前
C++编译期矩阵运算
开发语言·c++·算法
yy我不解释42 分钟前
关于comfyui的mmaudio音频生成插件时时间不一致问题(三)
开发语言·python·ai作画·音视频·comfyui
2301_8154829344 分钟前
C++中的类型标签分发
开发语言·c++·算法
SuperEugene44 分钟前
Vue3 模板语法规范实战:v-if/v-for 不混用 + 表达式精简,避坑指南|Vue 组件与模板规范篇
开发语言·前端·javascript·vue.js·前端框架