首頁 > 後端開發 > C++ > 主體

C++程式在陣列開頭加入元素

WBOY
發布: 2023-09-08 15:01:02
轉載
773 人瀏覽過

C++程式在陣列開頭加入元素

透過使用陣列和資料結構,可以在多個記憶體位置上儲存同質(相同)資料。使用陣列的關鍵好處是我們可以使用索引參數從任何位置檢索它們。這種資料結構變得線性,因為資料必須逐步插入和提取。我們只需要將該元素的索引或位置號碼放在方括號內,就可以從陣列中檢索它。在本文中,我們將使用陣列A和另一個元素e。我們將在C 中將e插入到A的起始位置。

理解概念並舉例

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

在上面的範例中,我們有一個包含九個元素的陣列A。我們將在陣列A的開頭插入另一個元素23。結果陣列包含了所有元素以及開頭的23。要在開頭插入一個元素,我們必須將所有元素向右移動一個位置,然後第一個插槽將為空,我們將新元素放在該位置。讓我們看一下演算法,以便更清楚地理解。

演算法

  • 取一個陣列 A 和一個元素 e

  • 如果陣列 A 有足夠的空間來插入元素 e,則

    • 對於從n-1到0的範圍內的i,執行以下操作:

      • A[ i 1 ] = A[ i ]

    • #結束迴圈

    • A[0]=e

    • 增加 n 1

  • #結束如果

  • 回傳 A

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 insertAtBeginning( int A[], int &n, int e ){
   if( n + 1 < Z ) {
      for( int i = n - 1; i >= 0; i-- ) {
         A[ i + 1 ] = A[ i ];
      }
      A[ 0 ] = e;
      n = n + 1;
   } 
}

int main() {
   int A[ Z ] = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
   int n = 12;
   
   cout << "Array before insertion: ";
   displayArr( A, n );
   
   cout << "Inserting 58 at the beginning:" << endl;
   insertAtBeginning( A, n, 58 );
   
   cout << "Array after insertion: ";
   displayArr( A, n );
   
   cout << "Inserting 225 at the beginning:" << endl;
   insertAtBeginning( A, n, 225 );
   
   cout << "Array after insertion: ";
   displayArr( A, n );
}
登入後複製

輸出

Array before insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Inserting 58 at the beginning:
Array after insertion: 58, 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Inserting 225 at the beginning:
Array after insertion: 225, 58, 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14,
登入後複製
登入後複製

使用向量在前面插入一個元素

向量是一種動態資料結構,它是C STL的一部分。我們可以在向量中獲得與陣列類似的功能。但是在向量中,我們只能在末尾或後面插入。沒有直接的方法可以在開頭插入。然而,我們可以像之前一樣將元素向後移動一個位置,然後在開頭插入新元素。或者我們可以建立另一個只包含新元素的單元素向量,然後將它們連接起來。因此,結果向量將包含所有先前的元素和新元素在開頭。讓我們看一下演算法和C 實作。

演算法

  • 取一個陣列 A 和一個元素 e

  • 建立一個空向量 B

  • #將 e 插入 B 中

  • A := 將 B 與 A 連接起來(先 B 再 A)

  • #回傳 A

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;
}

vector<int> insertAtBeginning( vector<int> A, int e ){
   vector<int> B( 1 );
   B[ 0 ] = e;
   B.insert( B.end(), A.begin(), A.end() );
   return B;
}

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 beginning:" << endl;
   A = insertAtBeginning( A, 58 );
   
   cout << "Array after insertion: ";
   displayArr( A );
   
   cout << "Inserting 225 at the beginning:" << endl;
   A = insertAtBeginning( 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 beginning:
Array after insertion: 58, 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Inserting 225 at the beginning:
Array after insertion: 225, 58, 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14,
登入後複製
登入後複製

結論

在本文中,我們已經看到如何在陣列的開頭插入元素。這裡我們討論了兩種不同的解決方案。第一種解決方案使用了靜態的C 數組,而第二種解決方案使用了向量。向量沒有直接在開頭插入元素的方法。我們可以直接在末尾插入元素使用push_back()方法。為此,我們使用了一個技巧,建立一個大小為1的數組,然後將新元素插入其中。然後將其與給定的數組連接起來。我們可以使用列表來實現相同的效果。然而,C 列表有push_front()方法,可以直接在前面插入元素。但是列表不是數組,它們是鍊錶。

以上是C++程式在陣列開頭加入元素的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!