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