C++程式:向陣列追加元素


一個陣列是一個線性順序資料結構,用於在連續的記憶體位置儲存同類資料。與其他資料結構一樣,陣列也必須具備以某種高效方式插入、刪除、遍歷和更新元素的功能。C++中的陣列是靜態的。C++中也提供了一些動態陣列結構。對於靜態陣列,最多可以儲存Z個元素。到目前為止,我們已經向其中插入了n個元素。在本文中,我們將學習如何在C++中將元素插入陣列的末尾(也稱為追加元素)。

透過示例理解概念

this關鍵字的使用方法如下

Given array A = [10, 14, 65, 85, 96, 12, 35, 74, 69]
After inserting 23 at the end, the array will look like this:
[10, 14, 65, 85, 96, 12, 35, 74, 69, 23]

在上面的例子中,假設我們有一個數組A,它最多可以容納50個元素。因此,Z的值為50。現在假設一開始,我們有9個元素。因此,陣列的大小n為9。要將另一個元素(例如,在我們的例子中為23)插入到陣列的末尾,該元素將被放置在末尾,並且A中的元素數量將增加1。因此,n變為10。由於我們在末尾插入,因此該過程很簡單。我們只需在所有元素之後新增新元素,而無需更改陣列中任何現有元素的位置。現在讓我們看看演算法以及C++實現程式碼,以便更好地理解。

演算法

  • 將陣列A、元素個數n和將插入到A中的元素e作為輸入。

  • 如果n < Z,其中Z是A中可以插入的最大位置數

    • A[n] = e

  • 結束if

  • 將n增加為n := n + 1

  • 返回陣列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;
}

int main() {
   int arr[ Z ] = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
   int n = 12;
   
   cout << "Array before insertion: ";
   displayArr( arr, n );
   
   cout << "Inserting 58 at the end:" << endl;
   insertAtEnd( arr, n, 58 );
   
   cout << "Array after insertion: ";
   displayArr( arr, n );
   
   cout << "Inserting 225 at the end:" << endl;
   insertAtEnd( arr, n, 225 );
   
   cout << "Array after insertion: ";
   displayArr( arr, n );
}

輸出

Array before insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Inserting 58 at the end:
Array after insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 58, 
Inserting 225 at the end:
Array after insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 58, 225,

使用向量追加元素

向量是C++ STL提供的動態資料結構。我們也可以在向量中獲得類似於陣列的功能。在向量中,我們可以使用`**push_back()**`函式在末尾插入元素。`**push_back**`函式將新元素作為引數,並將該元素插入到給定向量的末尾。該演算法很簡單。我們不需要做任何特殊的事情,只需透過傳遞我們將要插入的新元素來呼叫給定向量物件的函式即可。讓我們直接看看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;
}

vector<int> insertAtEnd( vector<int> A, int e ){
   A.push_back( e );
   return A;
}

int main() {
   vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};  
   cout << "Array before insertion: ";
   displayArr( A );
   
   cout << "Inserting 58 at the end:" << endl;
   A = insertAtEnd( A, 58 );
   
   cout << "Array after insertion: ";
   displayArr( A );
   
   cout << "Inserting 225 at the end:" << endl;
   A = insertAtEnd( A, 225 );
   
   cout << "Array after insertion: ";
   displayArr( A );
}

輸出

Array before insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Inserting 58 at the end:
Array after insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 58, 
Inserting 225 at the end:
Array after insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 58, 225,

結論

陣列是最簡單的連續儲存同類資料的資料結構之一。陣列是一種資料結構。像其他資料結構一樣,我們也可以輕鬆地插入、刪除、更新和遍歷陣列元素。在本文中,我們看到了兩種在末尾插入元素,換句話說,將元素追加到陣列中的方法。在第一種方法中,我們使用C++中的靜態陣列。由於我們的目標是末尾位置,因此我們不需要移動陣列中的任何元素,只需在最後一個索引處新增一個新元素,併為以後的使用增加總專案計數引數即可。在第二種情況下,我們使用向量。向量類似於C++中的普通陣列,但它們是動態的。它會在需要時自動更新其總大小。C++ STL支援向量,它有一個名為push_back()的特殊函式,用於在末尾插入元素。但是,我們不能用這種直接的方法在開頭新增元素。

更新於:2023年10月4日

44K+ 瀏覽量

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告