您好,欢迎访问一九零五行业门户网

获取数组中最后给定数量的项目的C++程序

数组是专门用于在一系列内存区域中保留同类型数据的数据结构。使用数组的主要好处是我们可以使用索引参数从任何位置访问它们。然而,插入和删除数据需要顺序操作,这将使得这个数据结构变成线性数据结构。我们可以简单地使用方括号中的索引或位置号来从数组中提取元素。本文将演示如何在c++中从数组中读取最近的k个数字。
理解概念并通过示例进行说明given array a = [10, 14, 65, 85, 96, 12, 35, 74, 69]we have another number k = 4the number of elements in a is 9the output will be the last k elements from a, which are:12, 35, 74, 69
we have the elements inside the array for every array, and the quantity n is also crucial. the number n indicates how many valid elements are there in an array. the size of the array might not match the n. although an array can have a maximum of z elements, only n of them must be valid; the remaining slots are empty. in this case, k must be less than or equal to n in order to retrieve the kth element from the array. before taking up the components, we must inspect it. for a better understanding, let's have a look at the algorithm.
算法读取一个数组 a 作为输入。同时接受元素的数量:n 和 k,以读取 a 中的前 k 个元素
创建一个空数组 b
如果 k < n,则
for i in range 0 to k - 1, do
b[ i ] = a[ n - k + i ]
end for
end if
返回 b
example#include <iostream># define z 50using namespace std;void displayarr(int arr[], int n){ for( int i = 0; i < n; i++ ){ cout << arr[ i ] << , ; } cout << endl;}void picklastkelement( int a[], int n, int b[], int &m, int k) { if( k <= n ){ for( int i = 0; i < k; i++ ) { b[ i ] = a[ n - k + i ]; m = m + 1; } }}int main() { int a[ z ] = {57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14}; int n = 12; int b[ z ]; int m = 0; cout << given array: ; displayarr( a, n ); picklastkelement( a, n, b, m, 7 ); cout << the last 7 element from a: ; displayarr( b, m ); m = 0; picklastkelement( a, n, b, m, 10 ); cout << the last 10 element from a: ; displayarr( b, m );}
outputgiven array: 57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14, the last 7 element from a: 52, 86, 14, 76, 65, 32, 14, the last 10 element from a: 44, 19, 86, 52, 86, 14, 76, 65, 32, 14,


使用向量在上述方法中,静态数组用于存储和检索数组元素。使用向量也可以实现相同的功能。向量是c++ stl的一部分,它是动态数组。让我们来看看代码。算法保持不变。
example#include <iostream>#include <vector># define z 50using namespace std;void displayarr( vector<int> v ){ for( int i = 0; i < v.size() ; i++ ){ cout << v[ i ] << , ; } cout << endl;}vector<int> picklastkelement( vector<int> a, int k) { vector<int> b; if( k <= a.size() ){ for( int i = 0; i < k; i++ ) { b.push_back( a[ a.size() - k + i ] ); } } return b;}int main() { vector<int> a = {57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14}; vector<int> b; cout << given array: ; displayarr( a ); b = picklastkelement( a, 7 ); cout << the last 7 element from a: ; displayarr( b ); b = picklastkelement( a, 10 ); cout << the last 10 element from a: ; displayarr( b ); }
outputgiven array: 57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14, the last 7 element from a: 52, 86, 14, 76, 65, 32, 14, the last 10 element from a: 44, 19, 86, 52, 86, 14, 76, 65, 32, 14,


使用向量构造函数最后一种方法是手动创建一个空向量,然后逐个复制元素。然而,我们可以直接使用向量迭代器在向量构造函数中复制最后k个元素。让我们看一下代码来理解这个概念。
example#include <iostream>#include <vector># define z 50using namespace std;void displayarr( vector<int> v ){ for( int i = 0; i < v.size() ; i++ ){ cout << v[ i ] << , ; } cout << endl;}vector<int> picklastkelement( vector<int> a, int k) { vector<int> b( a.begin() + (a.size() - k), a.end() ); return b;}int main() { vector<int> a = {57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14}; vector<int> b; cout << given array: ; displayarr( a ); b = picklastkelement( a, 7 ); cout << the last 7 element from a: ; displayarr( b ); b = picklastkelement( a, 10 ); cout << the last 10 element from a: ; displayarr( b ); }
outputgiven array: 57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14, the last 7 element from a: 52, 86, 14, 76, 65, 32, 14, the last 10 element from a: 44, 19, 86, 52, 86, 14, 76, 65, 32, 14,


在这里,使用a向量的最后k个元素创建了b向量。使用begin() 方法获取第一个项的地址,并使用偏移量begin() (a.size() − k)作为结束点,这样就可以指向最后k个元素。
conclusion本文介绍了从给定数组中读取或选择最后n个数字的三种不同方法。第二种和第三种解决方案基于向量,而不是第一种方法使用的静态默认数组。前两个问题的答案很简单。我们使用for循环逐个复制最后k个元素。最后一种技术是最简单的,它使用向量构造函数通过使用另一个向量的迭代器来复制组件来生成一个向量。
以上就是获取数组中最后给定数量的项目的c++程序的详细内容。
其它类似信息

推荐信息