C++程式:將一個數組推入另一個數組


陣列是一種線性的順序資料結構,用於在一系列記憶體區域中儲存同構資料。與其他資料結構一樣,陣列需要具備某些特性才能有效地插入、刪除、遍歷和更新元素。在C++中,我們的陣列是靜態的。此外,C++還提供了一些動態陣列結構。靜態陣列最多可以儲存Z個元素。目前其中有n個元素。在本文中,我們將瞭解如何在C++中將一個數組的元素推入另一個數組。

透過示例理解概念

Given array A = [10, 14, 65, 85, 96, 12, 35, 74, 69]
Another given array B = [56, 42, 15, 18, 20, 32]

Pushing the elements of B into A, signifies that A becomes:
A = [10, 14, 65, 85, 96, 12, 35, 74, 69, 56, 42, 15, 18, 20, 32]

在上面的例子中,很明顯我們有兩個陣列A和B。將B推入A意味著將B中存在的所有元素插入到陣列A中。這些元素將新增到A的末尾。但是要實現這一點,我們必須檢查A中剩餘的槽位(即A的最大大小 - A中現有元素的數量)是否等於或大於B中元素的數量。否則,我們無法將它們推入A中。讓我們看看演算法以及C++實現程式碼。

演算法

  • 將陣列A和B作為輸入,A中元素的數量n作為輸入,B中元素的數量m作為輸入

  • 如果A有足夠的空間容納整個B,則

  • 對於B中的每個元素e,執行

  • 將e新增到陣列A

  • 結束迴圈

  • 結束if

  • 返回陣列A和新的大小n

示例

#include <iostream>
# define Z 50

using namespace std;

void displayArr(int arr[], int n){
   for( int i = 0; i < n; i++ ){
      cout << arr[ i ] << ", ";
   }
   cout << endl;
}
void insertAtEnd( int arr[], int &n, int e ){
   if( n < Z ) {
      arr[ n ] = e;
   }
   n = n + 1;
}

void pushArrayToAnother( int A[], int &n, int B[], int m ) {
   if( (Z - n) >= m ){
      for( int i = 0; i < m; i++ ) {
         insertAtEnd( A, n, B[i] );   
      }
   }
}

int main() {
   int A[ Z ] = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
   int n = 12;
   
   int B[ Z ] = {56, 84, 23, 12, 17, 19, 65, 32};
   int m = 8;
   
   cout << "First Array: ";
   displayArr( A, n );
   
   cout << "Second Array: ";
   displayArr( B, m );
   
   pushArrayToAnother( A, n, B, m );
   cout << "Array A after pushing B:" << endl;
   displayArr( A, n );
}

輸出

First Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Second Array: 56, 84, 23, 12, 17, 19, 65, 32, 
Array A after pushing B:
57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 56, 84, 23, 12, 17, 19, 65, 32,

使用動態陣列或向量

可以使用向量來完成相同的事情。向量是C++ STL中存在的動態陣列。如果我們考慮向量,我們不需要關心可用的插入元素的空間。因為向量是動態的。它會在需要時自動新增新的槽位。演算法相同,無需檢查可用槽位。

演算法

  • 將陣列A和B作為輸入,A中元素的數量n作為輸入,B中元素的數量m作為輸入

  • 對於B中的每個元素e,執行

    • 將e新增到陣列A

  • 結束迴圈

  • 返回陣列A和新的大小n

示例

#include <iostream>
#include <vector>
# define Z 50

using namespace std;

void displayArr( vector<int> v ){
   for( int i = 0; i < v.size() ; i++ ) {
      cout << v[ i ] << ", ";
   }
   cout << endl;
}

void pushArrayToAnother( vector<int> &A, vector<int> B ){
   for( int i = 0; i < B.size() ; i++ ) {
      A.push_back( B[i] );
   }
}

int main(){
   vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};

   vector<int> B = {56, 84, 23, 12, 17, 19, 65, 32};

   cout << "First Array: ";
   displayArr( A );

   cout << "Second Array: ";
   displayArr( B );

   pushArrayToAnother( A, B );
   cout << "Array A after pushing B:" << endl;
   displayArr( A );
}

輸出

First Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Second Array: 56, 84, 23, 12, 17, 19, 65, 32, 
Array A after pushing B:
57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 56, 84, 23, 12, 17, 19, 65, 32,

使用向量中的insert()函式

以前的方法是一個手動過程。但是,我們可以使用向量STL中的insert()函式來完成相同的事情。insert()函式接收一個位置指標(使用迭代器)和一個迭代器,從一個容器物件複製元素並將元素從位置索引複製到另一個容器物件。讓我們看看C++實現,以便更清晰地瞭解。

示例

#include <iostream>
#include <vector>
# define Z 50

using namespace std;

void displayArr( vector<int> v ){
   for( int i = 0; i < v.size() ; i++ ){
      cout << v[ i ] << ", ";
   }
   cout << endl;
} 

void pushArrayToAnother( vector<int> &A, vector<int> B ) { 
   A.insert( A.end(), B.begin(), B.end() ); 
}

int main() {
   vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
   
   vector<int> B = {56, 84, 23, 12, 17, 19, 65, 32};
   
   cout << "First Array: ";
   displayArr( A );
   
   cout << "Second Array: ";
   displayArr( B );
   
   pushArrayToAnother( A, B );
   cout << "Array A after pushing B:" << endl;
   displayArr( A );
}

輸出

First Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Second Array: 56, 84, 23, 12, 17, 19, 65, 32, 
Array A after pushing B:
57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 56, 84, 23, 12, 17, 19, 65, 32,

結論

在本文中,我們已經看到了幾種不同的方法來將一個數組元素插入或推入另一個數組的末尾。在第一個示例中,使用了簡單的C++陣列,我們需要特別注意靜態陣列內的可用空間。在接下來的兩種方法中,我們不需要關心這一點,因為我們使用的是向量,它是動態的,並且會在需要時自動分配空間。

更新於:2022年12月13日

6K+ 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.