字典序字符串比较是指字符串按照字典顺序进行比较。例如,如果有两个字符串'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,则<li>这两个是相同的</li>否则,当cmp为正时,那么s is larger than t否则,当cmp为负数时,那么<li>s比t小</li><li>end if</li>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 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( <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 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( <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 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++程序比较两个字符串的字典序的详细内容。