首頁 >後端開發 >C++ >C++程式將一個陣列推入另一個陣列中

C++程式將一個陣列推入另一個陣列中

WBOY
WBOY轉載
2023-09-04 13:37:061359瀏覽

C++程式將一個陣列推入另一個陣列中

A linear sequential data structure called an array is used to store homogeneous data in a series of memory regions. An array needs to have certain features to insert, deleteions, traverse, and upleup effectively, just like other data structures do. Our arrays in C are static. In addition, C offers a few dynamic array structures. There may be a maximum of Z elements that can be store inside a statre array. in it. In this article, we will see how to push the elements of one array inside another array in 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 實作程式碼。

Algorithm

  • take the array A and B as input, the number of elements n in A as input, the number of elements m in B as input

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

  • for each element e in B, do

  • #append e to array A

  • 結束迴圈

  • end if

  • #return array A and new size n

#Example

#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,

Using Dynamic Arrays or Vectors

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

Algorithm

  • take the array A and B as input, the number of elements n in A as input, the number of elements m in B as input

  • for each element e in B, do

    • append e to array A

  • 結束迴圈

  • return array A and new size n

#Example

#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()函數

之前的方法是一個手動的過程。然而,我們可以使用vector STL中的insert()函數來實現相同的功能。 insert()函數接受一個位置指標(使用迭代器)和一個迭代器,從一個容器物件複製元素並將其從位置索引插入到另一個容器物件中。讓我們來看看C 的實作以獲得清晰的視圖。

Example

#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,

Conclusion

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

以上是C++程式將一個陣列推入另一個陣列中的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除