C++程式將字串傳遞給函數
任何使用函數的程式語言都具有更簡單、更模組化且在偵錯時更容易更改的程式碼。函數是模組化程式碼中非常有益的組成部分。函數可以接受參數並對其執行某些操作。與其他原始資料類型一樣,我們也可以將物件類型或陣列作為參數傳遞。在本文中,我們將看到如何在C 中將字串類型的資料作為函數參數傳遞。
傳遞類似C 字串的參數給函數
C supports stronger string objects which is actually a class with different member functions associated with them. A string object passing as an argument is similar to the passing of normal primitive normals.
Syntax
<return type> function_name ( string argument1, string argument2, … ) {
// function body
}
登入後複製
In the following example, we will see a program to check whether a given string is a palindrome or not. There will be two functions, one will reverse the string, and another will check notether the string 是 whe pal. us see the algorithm and corresponding C implementation.<return type> function_name ( string argument1, string argument2, … ) { // function body }
演算法
- define a function reverse(), this will take a string s
- n := floor of (length of s / 2)
- for i ranging from 0 to n/2; do
-
- temp := s[i]
- s[i] := s[ n - i - 1 ]
- s[ n - i - 1 ] := temp
- return True
- return False
的中文翻譯為:
範例
#include <iostream>
#include <sstream>
using namespace std;
string reverse( string s ) {
char temp;
int n = s.length();
for( int i = 0; i < n / 2; i++ ) {
temp = s[i];
s[i] = s[ n - i - 1 ];
s[ n - i - 1 ] = temp;
}
return s;
}
string isPalindrome( string s ) {
string revS = reverse( s );
if( s == revS ) {
return "True";
}
else {
return "False";
}
}
int main()
{
cout << "Is "racecar" a palindrome? " << isPalindrome( "racecar" ) << endl;
cout << "Is "abcdef" a palindrome? " << isPalindrome( "abcdef" ) << endl;
cout << "Is "madam" a palindrome? " << isPalindrome( "madam" ) << endl;
cout << "Is "sir" a palindrome? " << isPalindrome( "sir" ) << endl;
}
登入後複製
Output#include <iostream> #include <sstream> using namespace std; string reverse( string s ) { char temp; int n = s.length(); for( int i = 0; i < n / 2; i++ ) { temp = s[i]; s[i] = s[ n - i - 1 ]; s[ n - i - 1 ] = temp; } return s; } string isPalindrome( string s ) { string revS = reverse( s ); if( s == revS ) { return "True"; } else { return "False"; } } int main() { cout << "Is "racecar" a palindrome? " << isPalindrome( "racecar" ) << endl; cout << "Is "abcdef" a palindrome? " << isPalindrome( "abcdef" ) << endl; cout << "Is "madam" a palindrome? " << isPalindrome( "madam" ) << endl; cout << "Is "sir" a palindrome? " << isPalindrome( "sir" ) << endl; }
#
Is "racecar" a palindrome? True
Is "abcdef" a palindrome? False
Is "madam" a palindrome? True
Is "sir" a palindrome? False
登入後複製登入後複製
Passing C-like character array to a function#Since C supports almost all that is supported by C, we can also define strings using a character array like C. To pass C-like strings to a function, it must pass a character array ordacter array ordacter baseer baseer baseer of the string. The syntaxes are like below −#SyntaxIs "racecar" a palindrome? True Is "abcdef" a palindrome? False Is "madam" a palindrome? True Is "sir" a palindrome? False
(使用字元指標)
<return type> function_name ( char* <string variable>, … ) { // function body }
(使用字元陣列)
<return type> function_name ( char <string variable>[], … ) { // function body }
Example
的中文翻譯為:範例
#include <iostream> #include <cstring> #include <cstdlib> using namespace std; void reverse( char s[] ) { char temp; int n = strlen( s ); for( int i = 0; i < n / 2; i++ ) { temp = s[i]; s[i] = s[ n - i - 1 ]; s[ n - i - 1 ] = temp; } } string isPalindrome( char* s ) { char* sRev = (char*) malloc( strlen(s) ); strcpy( sRev, s ); reverse( sRev ); if( strcmp( sRev, s ) == 0 ) { return "True"; } else { return "False"; } } int main() { string s = "racecar"; cout << "Is "racecar" a palindrome? " << isPalindrome( const_cast<char*> (s.c_str()) ) << endl; s = "abcdef"; cout << "Is "abcdef" a palindrome? " << isPalindrome( const_cast<char*> (s.c_str()) ) << endl; s = "madam"; cout << "Is "madam" a palindrome? " << isPalindrome( const_cast<char*> (s.c_str()) ) << endl; s = "sir"; cout << "Is "sir" a palindrome? " << isPalindrome( const_cast<char*> (s.c_str()) ) << endl; }
Output
#Is "racecar" a palindrome? True Is "abcdef" a palindrome? False Is "madam" a palindrome? True Is "sir" a palindrome? False
在這個範例中,我們看到在C 中調整C樣式字串有幾個步驟。對於C樣式字串,使用cstring庫來取得長度、字串比較和其他操作。從C 字串到C字串的轉換,需要使用c_str()函數,但是這個函數傳回const char*,然而我們的函數只接受char*類型的資料。對於這種情況,我們需要使用const_cast將值轉換為char*類型。
Conclusion
函數可以接受原始資料型別以及陣列、物件類型等。當我們使用字串時,在C 中它們是物件類型,而在C中是字元陣列類型。但由於C 也支援C語法,所以在C 中也是有效的。傳遞一個字串物件很簡單,但是傳遞一個字元陣列需要特別注意並遵循一些嚴格的步驟。 C風格的字串可以以陣列格式或字元指標的形式傳遞。當我們知道函數會改變字串本身時,我們必須將字串作為字元陣列傳遞,否則,從指標修改字串是不允許的。當字串只被使用時,我們可以使用指標或字元陣列進行傳遞,效果是相同的。但在這種情況下,透過字元數組傳遞是好的,因為它會阻止對字串的無意更新。
以上是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)

Go語言提供了兩種動態函數創建技術:closures和反射。 closures允許存取閉包作用域內的變量,而反射可使用FuncOf函數建立新函數。這些技術在自訂HTTP路由器、實現高度可自訂的系統和建置可插拔的元件方面非常有用。

在C++函數命名中,考慮參數順序至關重要,可提高可讀性、減少錯誤並促進重構。常見的參數順序約定包括:動作-物件、物件-動作、語意意義和遵循標準函式庫。最佳順序取決於函數目的、參數類型、潛在混淆和語言慣例。

1. SUM函數,用於對一列或一組單元格中的數字進行求和,例如:=SUM(A1:J10)。 2、AVERAGE函數,用於計算一列或一組儲存格中的數字的平均值,例如:=AVERAGE(A1:A10)。 3.COUNT函數,用於計算一列或一組單元格中的數字或文字的數量,例如:=COUNT(A1:A10)4、IF函數,用於根據指定的條件進行邏輯判斷,並返回相應的結果。

C++函數中預設參數的優點包括簡化呼叫、增強可讀性、避免錯誤。缺點是限制靈活性、命名限制。可變參數的優點包括無限彈性、動態綁定。缺點包括複雜性更高、隱式型別轉換、除錯困難。

C++中的函數傳回參考類型的好處包括:效能提升:引用傳遞避免了物件複製,從而節省了記憶體和時間。直接修改:呼叫方可以直接修改傳回的參考對象,而無需重新賦值。程式碼簡潔:引用傳遞簡化了程式碼,無需額外的賦值操作。

自訂PHP函數與預定義函數的差異在於:作用域:自訂函數僅限於其定義範圍,而預定義函數可在整個腳本中存取。定義方式:自訂函數使用function關鍵字定義,而預先定義函數則由PHP核心定義。參數傳遞:自訂函數接收參數,而預先定義函數可能不需要參數。擴充性:自訂函數可以根據需要創建,而預定義函數是內建的且無法修改。

C++中的異常處理可透過自訂異常類別增強,提供特定錯誤訊息、上下文資訊以及根據錯誤類型執行自訂操作。定義繼承自std::exception的異常類,提供特定的錯誤訊息。使用throw關鍵字拋出自訂異常。在try-catch區塊中使用dynamic_cast將捕獲到的異常轉換為自訂異常類型。在實戰案例中,open_file函數會拋出FileNotFoundException異常,捕捉並處理該異常可提供更具體的錯誤訊息。
