C++程式遍歷陣列中的每個元素


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

透過示例理解概念

Given array A = [10, 14, 65, 85, 96, 12, 35, 74, 69]
Read all elements one by one
10
14
65
85
96
12
35
74
69

要遍歷陣列中的所有元素,我們可以簡單地執行一個迴圈,依次訪問索引並顯示陣列的內容。除了這種方法之外,沒有其他更簡單的方法。因此,演算法也很簡單,如下所示。

演算法

  • 獲取大小為 n 的陣列 A

  • 對於索引 i 從 0 到 n - 1,執行以下操作

    • 顯示 A[ i ]

  • 結束迴圈

示例

#include <iostream>
# define Z 50

using namespace std;

void displayArr(int arr[], int n){
   for( int i = 0; i < n; i++ ){
      cout << "Index: " << i << " Element: " << arr[ i ] << endl;
   } 
} 

int main() {
   int arr[ Z ] = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
   int n = 12;
   
   cout << "Array elements: " << endl;
   displayArr( arr, n );
}

輸出

Array elements: 
Index: 0 Element: 57
Index: 1 Element: 10
Index: 2 Element: 14
Index: 3 Element: 19
Index: 4 Element: 86
Index: 5 Element: 52
Index: 6 Element: 32
Index: 7 Element: 14
Index: 8 Element: 76
Index: 9 Element: 65
Index: 10 Element: 32
Index: 11 Element: 14

使用向量迭代器

與之前的靜態陣列方法不同,我們可以使用向量。向量是動態陣列。動態陣列可以在需要時增加其大小。我們也可以對向量使用與之前相同的方法。但是還有另一種更好的方法,它使用迭代器類。迭代器也隨 C++ STL 一起提供。讓我們看看使用迭代器訪問向量元素的程式碼。

示例

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

using namespace std;

void displayArr( vector<int> v ){
   vector<int>::iterator it;
   for( it = v.begin();  it != v.end() ; it++ ){
      cout << "Index: " << (it - v.begin()) << " Element: " << *it << endl;
   } 
}

int main() {
   vector<int> arr = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14}; 
   
   cout << "Array elements: " << endl;
   displayArr( arr );
}

輸出

Array elements: 
Index: 0 Element: 57
Index: 1 Element: 10
Index: 2 Element: 14
Index: 3 Element: 19
Index: 4 Element: 86
Index: 5 Element: 52
Index: 6 Element: 32
Index: 7 Element: 14
Index: 8 Element: 76
Index: 9 Element: 65
Index: 10 Element: 32
Index: 11 Element: 14

在這種方法中,建立了一個迭代器物件,該物件被分配給 v.begin(),它是向量的起始位置。當它不是 v.end() 時,列印 (it – v.begin()),這將返回元素的索引(因為“it”是當前元素的地址,而 v.begin() 是第一個元素的地址。因此,差值就是索引)。可以使用 *it 訪問“it”的值。

使用向量和 for-each 迴圈

此方法與上面顯示的任何其他方法沒有區別。唯一的區別在於迴圈。較新的 C++ 版本支援 for-each 迴圈,在該迴圈中,我們隱式地遍歷元素,而不是透過索引遍歷。讓我們看看程式碼示例以更好地理解。

示例

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

using namespace std;

void displayArr( vector<int> v ){ 
   for( int e : v ){
      cout << "Element: " << e << endl;
   } 
}

int main() {
   vector<int> arr = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14}; 
   
   cout << "Array elements: " << endl;
   displayArr( arr );
}

輸出

Array elements: 
Element: 57
Element: 10
Element: 14
Element: 19
Element: 86
Element: 52
Element: 32
Element: 14
Element: 76
Element: 65
Element: 32
Element: 14

結論

在本文中,我們看到了兩種不同的方法來讀取陣列中的所有元素。第一種是使用靜態陣列,我們透過訪問索引逐個遍歷每個元素。下一種方法是使用向量迭代器。向量迭代器就像列表、對映、集合等任何其他迭代器。可以使用迴圈逐個獲取元素。在 C++ 的後續版本中,我們可以獲得另一個有趣的 for 迴圈,即 for-each 迴圈。這不需要額外的索引或指標引用。它在本文件的最後一個示例中顯示。

更新於: 2022年12月13日

969 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.