C++程式:獲取陣列的最後一個元素
為了將相同型別的多個元素儲存在可順序訪問的位置或以允許順序訪問的方式,陣列是最佳選擇之一。幾乎任何計算機語言都提供陣列或相關的用於儲存資料的結構。由於插入、刪除、遍歷和更新等基本操作需要線性時間才能完成,因此陣列是線性資料結構。訪問陣列元素也很簡單。本文將演示如何在C++陣列中選擇最後一個元素。
透過示例理解概念
Given array A = [10, 14, 65, 85, 96, 12, 35, 74, 69] The last element is 69
例如,就像前面示例中給定的陣列一樣,可以使用索引位置訪問最後一個元素。C++(以及某些其他程式語言,如Java和Python)中的陣列索引從索引0開始。因此,要讀取最後一個索引,我們只需選擇索引 (n − 1) 處的元素,其中n是陣列的元素個數。
演算法
輸入一個數組A
n := A中的元素個數
last_element := 使用 A[n – 1] 獲取
返回 last_element
示例
#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;
}
int pickLastElement( int A[], int n) {
int last;
last = A[ n - 1 ];
return last;
}
int main() {
int A[ Z ] = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
int n = 12;
cout << "Given Array: ";
displayArr( A, n );
int last = pickLastElement( A, n );
cout << "The last element of A: " << last << endl;
int B[ Z ] = { 98, 12, 10, 23, 45, 74 };
int m = 6;
cout << "Another array: ";
displayArr( B, m );
last = pickLastElement( B, m );
cout << "The last element of B: " << last << endl;
}
輸出
Given Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, The last element of A: 14 Another array: 98, 12, 10, 23, 45, 74, The last element of B: 74
使用指標和基地址
陣列是基(**第一個**)位置地址加上偏移量(**索引**)。因此,另一種無需方括號訪問索引的方法是使用指標。要獲取最後一個元素,可以使用陣列的基地址值。讓我們來看一下實現,以便更清晰地瞭解。
示例
#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;
}
int pickLastElement( int A[], int n) {
int last;
last = *(A + n - 1);
return last;
}
int main() {
int A[ Z ] = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
int n = 12;
cout << "Given Array: ";
displayArr( A, n );
int last = pickLastElement( A, n );
cout << "The last element of A: " << last << endl;
int B[ Z ] = { 98, 12, 10, 23, 45, 74 };
int m = 6;
cout << "Another array: ";
displayArr( B, m );
last = pickLastElement( B, m );
cout << "The last element of B: " << last << endl;
}
輸出
Given Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, The last element of A: 14 Another array: 98, 12, 10, 23, 45, 74, The last element of B: 74
這裡,A的值(用指標*A表示)表示A指向的地址的值。這是陣列的基地址。
使用向量
向量是動態陣列,其他方面與陣列類似。在這裡,要讀取最後一個元素,我們只需訪問最後一個索引,即 vector.size() − 1。程式碼如下:
示例
#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;
}
int pickLastElement( vector<int> A) {
int last;
last = A[ A.size() - 1 ];
return last;
}
int main() {
vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
cout << "Given Array: ";
displayArr( A );
int last = pickLastElement( A );
cout << "The last element of A: " << last << endl;
vector<int> B = { 98, 12, 10, 23, 45, 74 };
cout << "Another array: ";
displayArr( B );
last = pickLastElement( B );
cout << "The last element of B: " << last << endl;
}
輸出
Given Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, The last element of A: 14 Another array: 98, 12, 10, 23, 45, 74, The last element of B: 74
使用向量back()函式
在前面的方法中,我們使用索引0來獲取元素,但還有另一種方法。我們可以使用**back()**方法,它返回最後一個元素。讓我們來看一下程式碼,以便更清晰地瞭解。
示例
#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;
}
int pickLastElement( vector<int> A) {
int last;
last = A.back();
return last;
}
int main() {
vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
cout << "Given Array: ";
displayArr( A );
int last = pickLastElement( A );
cout << "The last element of A: " << last << endl;
vector<int> B = { 98, 12, 10, 23, 45, 74 };
cout << "Another array: ";
displayArr( B );
last = pickLastElement( B );
cout << "The last element of B: " << last << endl;
}
輸出
Given Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, The last element of A: 14 Another array: 98, 12, 10, 23, 45, 74, The last element of B: 74
結論
對於從陣列讀取最後一個元素的方法,我們已經看到了四種不同的方法。前兩種基於C++中的靜態陣列實現。要讀取最後一個元素,我們只需獲取索引0處的元素。可以使用陣列基地址的指標完成相同操作。基地址指向第一個塊,該索引處的值將是第一個元素,透過新增偏移量,我們得到最後一個元素。在接下來的兩種方法中,我們使用了向量。這裡的方法與靜態陣列相同。最後一種方法使用向量的back()方法,該方法返回向量中的最後一個元素。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP