0

0

C++程序比较两个字符串的字典序

PHPz

PHPz

发布时间:2023-09-04 17:13:06

|

3911人浏览过

|

来源于tutorialspoint

转载

c++程序比较两个字符串的字典序

字典序字符串比较是指字符串按照字典顺序进行比较。例如,如果有两个字符串'apple'和'appeal',第一个字符串将排在后面,因为前三个字符'app'是相同的。然后对于第一个字符串,字符是'l',而在第二个字符串中,第四个字符是'e'。由于'e'比'l'短,所以如果我们按照字典顺序排列,它将排在前面。

在安排之前,字符串按字典顺序进行比较。在本文中,我们将看到 使用C++进行按字典顺序比较两个字符串的不同技术。

在C++字符串中使用compare()函数

C++ string对象有一个compare()函数,它接受另一个字符串作为输入并进行比较。

比较当前字符串与第二个字符串。当两个字符串相同时,此函数将返回0 字符串相同时,当第一个字符串较大时,它将返回一个负数(-1) 当第一个字符串较小时,将其翻译为中文:

当第一个字符串较小时,为正数(+1)。

语法

.compare(  )

让我们来看看C++中的算法和相应的实现。

MD5校验和计算小程序(C)
MD5校验和计算小程序(C)

C编写,实现字符串摘要、文件摘要两个功能。里面主要包含3个文件: Md5.cpp、Md5.h、Main.cpp。其中Md5.cpp是算法的代码,里的代码大多是从 rfc-1321 里copy过来的;Main.cpp是主程序。

下载

立即学习C++免费学习笔记(深入)”;

算法

  • 将两个字符串s和t作为输入
  • cmp := 使用 s.compare() 函数,参数为 t
  • 如果cmp等于0,则
    • 这两个是相同的
  • 否则,当cmp为正时,那么
    • s is larger than t
  • 否则,当cmp为负数时,那么
    • s比t小
  • end if

Example

#include 
using namespace std;
string solve( string s, string t ){
   int ret;
   ret = s.compare( t );
   if( ret == 0 ) {
      return s + " and " + t + " are the same";
   } else if( ret > 0 ) {
      return s + " is larger than " + t;
   } else {
      return s + " is smaller than " + t;
   }
}
int main(){
   string s = "apple";
   string t = "appeal";
   cout << "The result of comparison: " << solve( s, t ) << endl;
   s = "popular";
   t = "popular";
   cout << "The result of comparison: " << solve( s, t ) << endl;
   s = "Hello";
   t = "hello";
   cout << "The result of comparison: " << solve( s, t ) << endl;
}

输出

The result of comparison: apple is larger than appeal
The result of comparison: popular and popular are the same
The result of comparison: Hello is smaller than hello

在C风格的字符串中使用strcmp()函数

在C++中,我们也可以使用传统的C函数。C使用字符数组而不是字符串类型。

data. To compare two strings the strcmp() functions are used. This function takes two 将字符串作为参数。当它们相同时返回0。当第一个字符串小于第二个字符串时返回正值 一是当第二个值较大时,它是较大且为负的值。

语法

strcmp( ,  )

Example

#include 
#include 
using namespace std;
string solve( const char* s, const char* t ){
   int ret;
   ret = strcmp( s, t );
   if( ret == 0 ) {
      return string(s) + " and " + string(t) + " are the same";
   } else if( ret > 0 ) {
      return string(s) + " is larger than " + string(t);
   } else {
      return string(s) + " is smaller than " + string(t);
   }
}
int main(){
   string s = "apple";
   string t = "appeal";
   cout << "The result of comparison: " << solve( s.c_str() , t.c_str()) << endl;
   s = "popular";
   t = "popular";
   cout << "The result of comparison: " << solve( s.c_str() , t.c_str()) << endl;
   s = "Hello";
   t = "hello";
   cout << "The result of comparison: " << solve( s.c_str() , t.c_str()) << endl;
}

输出

The result of comparison: apple is larger than appeal
The result of comparison: popular and popular are the same
The result of comparison: Hello is smaller than hello

使用比较运算符

像数字数据一样,字符串也可以使用比较运算符进行比较。if-else conditions can be used directly for strings in C++.

语法

strcmp( ,  )

Example

#include 
using namespace std;
string solve( string s, string t ){
   int ret;
   if( s == t ) {
      return s + " and " + t + " are the same";
   } else if( s > t ) {
      return s + " is larger than " + t;
   } else {
      return s + " is smaller than " + t;
   }
}
int main(){
   string s = "apple";
   string t = "appeal";
   cout << "The result of comparison: " << solve( s, t ) << endl;
   s = "popular";
   t = "popular";
   cout << "The result of comparison: " << solve( s, t ) << endl;
   s = "Hello";
   t = "hello";
   cout << "The result of comparison: " << solve( s, t ) << endl;
}

输出

The result of comparison: apple is larger than appeal
The result of comparison: popular and popular are the same
The result of comparison: Hello is smaller than hello

结论

字符串比较是我们在多个应用程序中执行的重要任务。在C++中, 有几种不同的方法可以比较字符串。第一种是使用compare()方法 需要翻译的内容为:Which takes one string as input and checks with the current string. In C++ the comparison 运算符如(==)、(>)、(=)可以用于字符串比较。另一方面, C-like字符串可以使用strcmp()函数进行比较。该函数接受常数 character pointers. The compare() method and the strcmp() method returns 0 when both 第一个字符串较大时,返回一个正数;当两个字符串相同时,返回0 第一个较小,它将返回一个正数。

相关专题

更多
string转int
string转int

在编程中,我们经常会遇到需要将字符串(str)转换为整数(int)的情况。这可能是因为我们需要对字符串进行数值计算,或者需要将用户输入的字符串转换为整数进行处理。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

317

2023.08.02

java基础知识汇总
java基础知识汇总

java基础知识有Java的历史和特点、Java的开发环境、Java的基本数据类型、变量和常量、运算符和表达式、控制语句、数组和字符串等等知识点。想要知道更多关于java基础知识的朋友,请阅读本专题下面的的有关文章,欢迎大家来php中文网学习。

1465

2023.10.24

Go语言中的运算符有哪些
Go语言中的运算符有哪些

Go语言中的运算符有:1、加法运算符;2、减法运算符;3、乘法运算符;4、除法运算符;5、取余运算符;6、比较运算符;7、位运算符;8、按位与运算符;9、按位或运算符;10、按位异或运算符等等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

228

2024.02.23

php三元运算符用法
php三元运算符用法

本专题整合了php三元运算符相关教程,阅读专题下面的文章了解更多详细内容。

85

2025.10.17

if什么意思
if什么意思

if的意思是“如果”的条件。它是一个用于引导条件语句的关键词,用于根据特定条件的真假情况来执行不同的代码块。本专题提供if什么意思的相关文章,供大家免费阅读。

743

2023.08.22

js 字符串转数组
js 字符串转数组

js字符串转数组的方法:1、使用“split()”方法;2、使用“Array.from()”方法;3、使用for循环遍历;4、使用“Array.split()”方法。本专题为大家提供js字符串转数组的相关的文章、下载、课程内容,供大家免费下载体验。

257

2023.08.03

js截取字符串的方法
js截取字符串的方法

js截取字符串的方法有substring()方法、substr()方法、slice()方法、split()方法和slice()方法。本专题为大家提供字符串相关的文章、下载、课程内容,供大家免费下载体验。

208

2023.09.04

java基础知识汇总
java基础知识汇总

java基础知识有Java的历史和特点、Java的开发环境、Java的基本数据类型、变量和常量、运算符和表达式、控制语句、数组和字符串等等知识点。想要知道更多关于java基础知识的朋友,请阅读本专题下面的的有关文章,欢迎大家来php中文网学习。

1465

2023.10.24

高德地图升级方法汇总
高德地图升级方法汇总

本专题整合了高德地图升级相关教程,阅读专题下面的文章了解更多详细内容。

27

2026.01.16

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
php初学者入门课程
php初学者入门课程

共10课时 | 0.6万人学习

PHP基础入门课程
PHP基础入门课程

共33课时 | 1.9万人学习

Go语言教程-全程干货无废话
Go语言教程-全程干货无废话

共100课时 | 9.7万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号