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

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

相关推荐
Mapleay1 分钟前
CLion IDE 使用
c++
李白你好3 分钟前
Java GUI-未授权漏洞检测工具
java·开发语言
兵哥工控6 分钟前
MFC用高精度计时器实现五段时序控制的改进
c++·mfc
leo__52018 分钟前
拉丁超立方抽样(Latin Hypercube Sampling, LHS)MATLAB实现
开发语言·matlab
会编程的土豆18 分钟前
日常做题 vlog
数据结构·c++·算法
sycmancia20 分钟前
Qt——Qt中的标准对话框
开发语言·qt
22信通小白22 分钟前
USRP初学者使用手册(基础配置及bug记录)——Linux+Clion(单台X310收发)
linux·运维·c++·5g·bug·信息与通信
橙露37 分钟前
Python 对接 API:自动化拉取、清洗、入库一站式教程
开发语言·python·自动化
Omigeq43 分钟前
1.4 - 曲线生成轨迹优化算法(以BSpline和ReedsShepp为例) - Python运动规划库教程(Python Motion Planning)
开发语言·人工智能·python·算法·机器人
2301_8084143844 分钟前
自动化测试的实施
开发语言·python