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


字典序字符串比较表示字符串按字典顺序进行比较。例如,如果存在两个字符串“apple”和“appeal”,则第一个字符串将排在后面,因为前三个字符“app”相同。然后,对于第一个字符串,字符是“l”,而在第二个字符串中,第 4 个字符是“e”。由于“e”比“l”小,因此如果我们按字典序排列它们,它将排在前面。

在排序之前,字符串会按字典序进行比较。在本文中,我们将了解使用 C++ 以字典序比较两个字符串的不同方法。

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

C++ 字符串对象具有 compare() 函数,该函数以另一个字符串作为输入,并将当前字符串与第二个字符串进行比较。当两个字符串相同时,此函数将返回 0;当第一个字符串较大时,将返回负数 (-1);当第一个字符串较小时,将返回正数 (+1)。

语法

<first string>.compare( <second string> )

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

算法

  • 将两个字符串 s 和 t 作为输入
  • cmp := 使用 s.compare() 函数,参数为 t
  • 如果 cmp 为 0,则
    • 这两个字符串相同
  • 否则,如果 cmp 为正,则
    • s 大于 t
  • 否则,如果 cmp 为负,则
    • s 小于 t
  • 结束 if

示例

Open Compiler
#include <iostream> 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 使用字符数组而不是 stringtype 数据。要比较两个字符串,可以使用 strcmp() 函数。此函数将两个字符串作为参数。当它们相同时返回 0。第一个字符串较大时返回正值,第二个字符串较大时返回负值。

语法

strcmp( <first string>, <second string> )

示例

Open Compiler
#include <iostream> #include <cstring> 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

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

使用比较运算符

与数字数据一样,字符串也可以使用比较运算符进行比较。在 C++ 中,可以将 if-else 条件直接用于字符串。

语法

strcmp( <first string>, <second string> )

示例

Open Compiler
#include <iostream> 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() 方法。它将一个字符串作为输入并与当前字符串进行检查。在 C++ 中,比较运算符(如 (==)、(>)、(<) (<=)、(>=) 可用于字符串比较。另一方面,类 C 字符串可以使用 strcmp() 函数进行比较。它接受常量字符指针。当两个字符串相同时,compare() 方法和 strcmp() 方法返回 0;当第一个字符串较大时,它返回正数;当第一个字符串较小时,它将返回负数。

更新于: 2022-12-14

8K+ 浏览量

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告