C++ 字串連線



字串連線是指將一個元素新增到現有元素的過程。在此上下文中,字串連線是指將兩個(或多個)字串新增到一起的方法。因此,結果字串是初始字串和新增字串的組合。

在 C++ 中有幾種連線字串的方法,其中一些如下所示:

  • 使用 string::append() 函式
  • 使用 '+' 運算子
  • 使用 strcat() 函式 用於 C 風格字串
  • 使用 for 迴圈
  • 使用 while 迴圈
  • 使用 基於範圍的 迴圈
  • 使用 繼承
  • 使用 OOPS 中的 友元函式

這些方法將在本章的後續文章中詳細解釋。因此,讓我們深入探討這些概念。

使用 string::append() 函式進行字串連線

String 是在 <string> 標頭檔案中定義的類,而 append() 函式是該類的繼承方法。此方法用於將給定字串附加或新增到初始字串。

語法

以下語法用於使用 append() 方法連線字串:

initial_string.append(new_string);
initial_string.append(“this is new”);

引數

string::append() 函式以字串作為引數。字串可以顯式傳遞或作為物件傳遞。

append() 函式示例

以下示例程式碼用於使用 append() 方法連線字串:

#include <iostream>
using namespace std;

int main() {
   string initial_string("I Love TP.");
   string new_string(" I am new here.");

   //using append function with object parameter 
   initial_string.append(new_string);

   //using append function with explicit string
   initial_string.append(" Could you help me?");

   cout << initial_string << endl;
   return 0;
}

輸出

I Love TP. I am new here. Could you help me?

使用 '+' 運算子進行字串連線

新增字串最簡單的方法之一是在兩個或多個字串上使用 '+' 運算子。這可以在原地(即,無需建立新字串)或在新的字串物件中完成。這是 C++ 程式語言的 新特性 之一。

語法

以下語法用於使用 '+' 運算子連線字串:

new_string=initial_string+added_string;
initial_string+=added_string

這裡,新字串可以在原地新增,也可以透過建立新的字串物件來新增。

示例

以下示例程式碼用於使用 '+' 運算子連線字串:

#include <iostream>
using namespace std;

int main() {
   string initial_string("I Love TP.");
   string new_string(" I am new here.");

   string a="Hey !!! " + initial_string;
   //using new string object

   a+=new_string;
   //inplace addition

   a+=" Could you help me? ";

   cout << a << endl;
   return 0;
}

輸出

Hey !!! I Love TP. I am new here. Could you help me? 

使用 for 迴圈進行字串連線

我們可以使用一個簡單的 for 迴圈,從新字串的開頭到新字串的結尾,並且在每次迭代中,我們可以將該字元新增到初始字串中。這可以在原地完成,也可以使用新的字串物件完成。這種型別的連線在 C 風格字串(即字元陣列)中也是可能的。

語法

以下語法用於使用 for 迴圈從字串開頭到結尾連線字串:

for(int i=0;i<s.length();i++){
   initial+=s[i];
}

示例

以下示例程式碼用於使用 for 迴圈從字串開頭到結尾連線字串:

#include <iostream>
using namespace std;

int main(){
   string initial_string("I Love TP.");
   string new_string(" I am new here.");

   for(int i=0;i<new_string.size();i++){
      initial_string+=new_string[i];
   }
   //using for loop to iterate over new_string

   cout << initial_string << endl;
   return 0;
}

輸出

I Love TP. I am new here.

使用 while 迴圈計算字串長度

我們還可以使用一個簡單的 while 迴圈。此迴圈執行直到我們到達字串的末尾,並且在每次迭代中,我們可以將相應的字元新增到初始字串中。這可以在原地完成,也可以使用新的 字串物件 完成。這種型別的連線在 C 風格字串(即字元陣列)中也是可能的。

語法

以下語法用於使用 while 迴圈從字串開頭到結尾連線字串:

for(int i=0;i<s.length();i++){
   initial+=s[i];
}

示例

以下示例程式碼用於使用 while 迴圈從字串開頭到結尾連線字串:

#include <iostream>
using namespace std;

int main() {
   string initial_string("I Love TP.");
   string new_string(" I am new here.");

   int i=0;
   while(new_string[i]!='\0'){
      initial_string+=new_string[i];
      i++;
   }
   //using while loop to iterate over new_string

   cout << initial_string << endl;
   return 0;
}

使用基於範圍的迴圈進行字串連線

我們還可以使用基於範圍的迴圈,它將自動迭代整個字串,並且我們可以將每個字元新增到初始字串中。這可以在原地完成,也可以使用新的字串物件完成。

語法

以下語法用於使用基於範圍的迴圈從字串開頭到結尾連線字串:

for(char c: s){
   initial+=c;
}

示例

以下示例程式碼用於使用基於範圍的迴圈從字串開頭到結尾連線字串:

#include <iostream>
using namespace std;

int main() {
   string initial_string("I Love TP.");
   string new_string(" I am new here.");

   for(char c: new_string){
      initial_string+=c;
   }
   //using range based loop for concatentation

   cout << initial_string << endl;
   return 0;
}

輸出

I Love TP. I am new here.

使用 strcat() 函式進行字串連線

我們可以使用 strcat() 函式在 C++ 中連線字串。但是,此方法不適用於字串物件,它僅適用於 C 風格字串,即字元陣列。此方法在 <string.h> 標頭檔案中定義。

語法

以下語法用於使用 strcat() 方法連線字串:

strcat(s1,s2);

引數

這裡,s1 和 s2 是兩個字元陣列(即字串),作為引數傳遞給 strcat() 方法。

示例

以下示例程式碼用於使用 strcat() 方法連線字串:

#include <bits/stdc++.h>
using namespace std;

int main(){
   char s1[]="I love ";
   char s2[]=" TP. Could you help me? ";

   //using strcat function to concatenate
   //result stored in s1

   strcat(s1,s2);
   cout << s1 << endl;
   return 0;
}

輸出

I love  TP. Could you help me? 

使用繼承進行字串連線

我們可以使用 strcat() 函式在 C++ 中連線字串。但是,此方法不適用於字串物件,它僅適用於 C 風格字串,即字元陣列。此方法在 <string.h> 標頭檔案中定義。

語法

以下語法用於在 OOP 概念中使用繼承方法連線字串:

strcat(s1,s2);

示例

以下示例程式碼用於在 OOP 概念中使用繼承方法連線字串:

#include <bits/stdc++.h>
using namespace std; 

//parent class
class parent { 
   public:
      virtual string concatenate(string s1, string s2) = 0;
      //creating a virtual method to inherit
}; 

//child class
class child: parent { 
   public: 
      string concatenate (string s1, string s2){
         s1+=s2;
         //using + operator to add strings
         return s1;
      } 
}; 

int main() { 
   child ch1;
   cout << ch1.concatenate ("I love ", "TP !!!"); 

   return 0; 
}

輸出

I love TP !!!

使用 OOPS 中的友元函式進行字串連線

友元類可以訪問在其宣告為友元的其他類的私有和受保護成員。有時允許特定類訪問其他類的私有和受保護成員非常有用。

因此,我們利用此友元類來宣告一個輔助方法,然後對 C 風格字串使用 strcat() 函式。

語法

以下語法用於在 C++ 中使用友元方法連線字串:

Class A {
   string s1=[value];
   string s2=[value];
   friend void helper(A obj);
}
helper(A) {
   strcat(obj.s1, obj.s2);
};

示例

以下示例程式碼用於在 C++ 中使用友元方法連線字串:

#include <bits/stdc++.h>
using namespace std; 

// concatenate class 
class concatenate { 
   public: 
      char s1[20] = "I love TP !!!";
      char s2[20] = "Hey... "; 

      friend void helper(concatenate par1);	 
}; 

void helper (concatenate par1) { 
   // Pass parameter to concatenate 
   strcat (par1.s2, par1.s1); 

   cout << par1.s2; 
} 

int main() { 

   // Create object of class 
   concatenate par1; 

   //pass this object to helper function 
   helper(par1); 

   return 0; 
}

輸出

Hey... I love TP !!!
廣告