字串複製函式(迭代和遞迴)


在 C++ 環境中,迭代語句是一個複合語句或原因語句,可以一次執行零次或多次。此過程受迴圈終止過程的約束。當存在 break 語句或語句的延續時,語句按特定順序執行。

C++ 中存在四種類型的迭代:

  • While

  • Do

  • For

  • 基於範圍的 for

此處提到的每個迴圈都會迭代特定條件,直到其終止。當語句時,表示式計算結果為零。迭代語句不能被視為宣告。在這些語句中,while 和 do 語句不適用增量。但是,對於 for 和基於範圍的 for,增量是執行方法的必須條件。

以下是迭代方法的一些特性:

  • 初始猜測 – 1

  • 開括號型別

  • 收斂速度非常快

  • 收斂是一個線性過程

  • 方法正在修改

  • 精度非常好

  • 程式設計工作非常簡單

遞迴是 C++ 中的一個特定函式,它以重複的方式直接或間接地呼叫自身,直到滿足特定條件。遞迴函式沒有基本情況。在遞迴中,有兩個情況可以執行程式碼

  • 遞迴條件 - 它有助於在迴圈中重複編碼操作。它節省了時間並降低了程式碼的複雜性。

  • 基本條件 - 它幫助條件終止過程。

C++ 中迭代的演算法

以下是用迭代方法複製字串的基本演算法,該方法將以重複的方式執行條件,直到其終止。

  • 步驟 1 - 開始

  • 步驟 2 - 讀取 x0 和 e 的值。(e 是所需的精度)

  • 步驟 3 - 計算 x1 = g(x0)

  • 步驟 4 - 如果 [x1 – x0] <= e,則轉到步驟 6

  • 步驟 5 - 否則,將 x0 = x1 並轉到步驟 3。

  • 步驟 6 - 顯示 x1 作為根。

  • 步驟 7 - 停止

C++ 中遞迴的演算法

以下是用遞迴方法複製 C++ 環境中字串的演算法,該方法將執行邏輯條件,直到其終止。

  • 步驟 1 - 開始

  • 步驟 2 - 定義基本情況。

  • 步驟 3 - 定義遞迴情況。

  • 步驟 4 - 確保遞迴終止。

  • 步驟 5 - 合併解決方案。

  • 傳送 6 - 結束

使用迭代複製函式的語法

template<class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first content, InputIterator last content, OutputIterator result content){
   while (first!=last) {
      *result = *first;
      ++result; ++first;
   }
   return result;
}

這是此語法,它複製特定的元素。該函式在目標的末尾返回一個迭代器作為值。

使用遞迴複製函式的語法

int main(){
   char str10[70], str20[80];
   printf("Enter string to copy for this operation: ");
   scanf("%[^\n]s", str10);
   copy(str10, str20, 0);
   printf("Copying success for the operation.\n");
   printf("The first string is here: %s\n", str10);
   printf("The second string is here: %s\n", str20);
   return 0;
}
void copy(char str1[], char str2[], int index) {
   str20[index] = str10[index];
   
   // printf ("INDEX IS %d\n", index);
   if (str1[index] == '\0')
   return;
   copy(str10, str20, index + 1);
}

這裡是在此語法中,透過使用遞迴來複制() 函式。子函式將控制值返回給該特定父函式,該值編碼在要複製到字串二的字串一中。以下是基本流程和過程:

char* strcpy(char* destination, const char* source);
void recurse() {
   ... .. ...
   recurse();
   ... .. ...
}
int main() {
   ... .. ...
   recurse();
   ... .. ...
}
  • 透過“void copy (char[],char[],int);”定義一個函式。

  • 此函式用於透過遞迴將一個字串複製到另一個字串。

  • 向字串新增值。

方法

  • 方法 1 - 使用 C++ 複製字串。

  • 方法 2 - 使用迭代方法將一個字串複製到另一個字串。

  • 方法 3 - 使用遞迴方法將一個字串複製到另一個字串。

使用 C++ 複製字串

這裡我們使用了 strcpy() 和 cstring 函式在 C++ 環境中複製字串。

示例 1:在 C++ 環境中複製字串物件

#include <iostream>
using namespace std;
int main() {
   string s07, s16;
   cout << "Enter data in string s07: ";
   getline (cin, s07);
   s16 = s07;
   cout << "s07 = "<< s07 << endl;
   cout << "s16 = "<< s16;
   return 0;
}

輸出

Enter data in string s07: s07 = 
s16 = 

示例 2:使用 C++ 複製 C 字串

#include <iostream>
#include <cstring>
using namespace std;
int main() {
   char s07[2022], s16[2022];
   cout << "Enter some data input in string s07: ";
   cin.getline(s07, 2022);
   strcpy(s16, s07);
   cout << "s07 = "<< s07 << endl;
   cout << "s16 = "<< s16;
   return 0;
}

輸出

Enter some data input in string s07: s07 = 
s16 =

使用迭代方法將一個字串複製到另一個字串

對於迭代,我們可以將每個字元內容從 s100 複製到 s200,從表示為 0 的特定索引開始。透過實現此方法,每次呼叫都會將每個索引增加 1。為此,s100 字串不會到達終止。時間複雜度為 O(m),其中 m 是字串的長度,輔助空間為 O(1),其中正在使用額外的空間。

示例 3

#include <bits/stdc++.h>
using namespace std;
// declare a function to override the method to a particular data string onanother void string
// lets assume we have enough space

void myCopy(char s100[], char s200[]) {
   int a = 0;
   for (a=0; s100[a] != '\0'; a++)
   s200[a] = s100[a];
   s200[a] = '\0';
}

// Declare the driver function to go further
int main() {
   char s100[2022] = "ARBRDDINDBD";
   char s200[2001] = "";
   myCopy (s100, s200);
   cout << s200;
   return 0;
}

輸出

ARBRDDINDBD

使用遞迴方法將一個字串複製到另一個字串

實現遞迴過程,我們可以將每個字元從字串 s1997 複製到字串 s2001。起始索引為 0,方法索引將增加 1,直到過程終止。對於此過程,時間複雜度為 O(m),其中 m 是特定字串的長度。由於遞迴呼叫棧,這裡的輔助空間也為 O(m)。

示例 4

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

// Apply function to copy one string in to other string by using recursion method
void myCopy(char s1997[], char s2001[], int index = 0) {

   // copying every character from the string s1997 to string s2001
   s2001[index] = s1997[index];

   // if the operated string reachs to the end then stop and terminate the process
   if (s1997[index] == '\0')
   return;

   // increase the string characters by one to stop the termination
   myCopy(s1997, s2001, index + 1);
}

// Declare the driver function to go for the next
int main() {
   char s1997[1000] = "RDDARBINDBD";
   char s2001[1000] = "";
   myCopy(s1997, s2001);
   cout << s2001;
   return 0;
}

輸出

RDDARBINDBD

結論

今天在這篇文章中,我們學習瞭如何在 C++ 環境中使用迭代和遞迴將字串資料從一個複製到另一個。這裡我們提到了可能的演算法並根據邏輯構建了 C++ 程式碼。希望這將幫助您對此處討論的主題有一個廣泛的瞭解。

更新於: 2023 年 4 月 5 日

467 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告