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

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

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

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

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

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

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

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

语法

.compare(  )

登录后复制

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

立即学习“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: apple is larger than appealThe result of comparison: popular and popular are the sameThe 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: apple is larger than appealThe result of comparison: popular and popular are the sameThe result of comparison: Hello is smaller than hello

登录后复制

使用比较运算符

像数字数据一样,字符串也可以使用比较运算符进行比较。if-elseconditions 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: apple is larger than appealThe result of comparison: popular and popular are the sameThe 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第一个较小,它将返回一个正数。

以上就是C++程序比较两个字符串的字典序的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2583867.html

(0)
上一篇 2025年3月6日 14:40:36
下一篇 2025年3月1日 23:44:05

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论