卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章17310本站已运行3326

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

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

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

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

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

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

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

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

语法

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

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

算法

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

Example

'
#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使用字符数组而不是字符串类型。

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

语法

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

Example

'
#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

使用比较运算符

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

语法

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

Example

'
#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()方法 需要翻译的内容为: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 第一个较小,它将返回一个正数。

卓越飞翔博客
上一篇: 使用Python进行主成分分析
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏