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 第一個較小,它將傳回一個正數。
以上是C++程式比較兩個字串的字典序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

給出以下是一個將羅馬數字轉換為十進制數字的C語言演算法:演算法步驟1-開始步驟2-在運行時讀取羅馬數字步驟3-長度:=strlen(roman)步驟4-對於i=0到長度-1 步驟4.1-switch(roman[i]) 步驟4.1.1-case'm': &nbs

字典序字串比較是指字串依照字典順序進行比較。例如,如果有兩個字串'apple'和'appeal',第一個字串將排在後面,因為前三個字元'app'是相同的。然後對於第一個字串,字元是'l',而在第二個字串中,第四個字元是'e'。由於'e'比'l'短,所以如果我們按照字典順序排列,它將排在前面。在安排之前,字串會按字典順序進行比較。在本文中,我們將看到使用C++進行按字典順序比較兩個字串的不同技術。在C++字串中使用compare()函數C++string物件有一個compare()

函數strcmp()是內建函式庫函數,在「string.h」頭檔中宣告。該函數用於比較字串參數。它按字典順序比較字串,這意味著它逐個字元地比較字串。它啟動comp

連結列表使用動態記憶體分配,即它們相應地增長和收縮。它們被定義為節點的集合。這裡,節點有兩個部分,即資料和鏈路。資料、連結和鍊錶的表示如下-鍊錶的類型鍊錶有四種類型,如下:-單鍊錶/單鍊錶雙/雙向鍊錶循環單鍊錶循環雙鍊錶我們使用遞歸方法求鍊錶長度的邏輯是-intlength(node *temp){ if(temp==NULL) returnl; else{&n

rename函數將檔案或目錄從舊名稱變更為新名稱。此操作類似於移動操作。因此,我們也可以使用此rename函數來移動檔案。此函數存在於stdio.h庫頭檔中。 rename函數的語法如下:intrename(constchar*oldname,constchar*newname);rename()函數的函數它接受兩個參數。一個是oldname,一個是newname。這兩個參數都是指向常數字元的指針,用於定義檔案的舊名稱和新名稱。如果檔案重新命名成功,則傳回零;否則,傳回非零整數。在重新命名操作期間

雙曲函數是使用雙曲線而不是圓定義的,與普通三角函數相當。它從提供的弧度角傳回雙曲正弦函數中的比率參數。但要做相反的事,或者換句話說。如果我們想要根據雙曲正弦值計算角度,我們需要像雙曲反正弦運算一樣的反雙曲三角運算。本課程將示範如何使用C++中的雙曲反正弦(asinh)函數,並使用雙曲正弦值(以弧度為單位)計算角度。雙曲反正弦運算遵循下列公式-$$\mathrm{sinh^{-1}x\:=\:In(x\:+\:\sqrt{x^2\:+\:1})},其中\:In\:是\:自然對數\:(log_e\:k)

映射是C++中的一種特殊類型的容器,其中每個元素都是一對兩個值,即鍵值和映射值。鍵值用於索引每個項目,映射值是與鍵關聯的值。無論映射值是否唯一,鍵始終是唯一的。要在C++中列印映射元素,我們必須使用迭代器。一組項目中的一個元素由迭代器物件指示。迭代器主要與陣列和其他類型的容器(例如向量)一起使用,並且它們具有一組特定的操作,可用於識別特定範圍內的特定元素。可以增加或減少迭代器來引用範圍或容器中存在的不同元素。迭代器指向範圍內特定元素的記憶體位置。使用迭代器在C++中列印地圖首先,我們先來看看如何定義

現代科學在很大程度上依賴複數的概念,而這個概念最初是透過GirolamoCardano在16世紀引入的17世紀初建立。複數的公式是a+ib,其中a保留html代碼且b是實數。一個複數被認為有兩個部分:實部<a>和虛部(<ib>)。 i或iota的值為√-1。 C++中的複數類別是用來表示複數的類別。 C++中的complex類別可以表示並控制幾個複數操作。我們來看看如何表示和控制顯示複數。 imag()成員函數如前所述,複數由實部和虛部兩部分組成。顯示實部我們使用real()
