数组是专门用于在一系列内存区域中保留同类型数据的数据结构。使用数组的主要好处是我们可以使用索引参数从任何位置访问它们。然而,插入和删除数据需要顺序操作,这将使得这个数据结构变成线性数据结构。我们可以简单地使用方括号中的索引或位置号来从数组中提取元素。本文将演示如何在C++中从数组中读取最近的k个数字。
Given array A = [10, 14, 65, 85, 96, 12, 35, 74, 69] We have another number k = 4 The number of elements in A is 9 The 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
#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 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 ); }
Given 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的一部分,它是动态数组。让我们来看看代码。算法保持不变。
#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> 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 ); }
Given 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个元素。让我们看一下代码来理解这个概念。
#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> 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 ); }
Given 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个元素。
本文介绍了从给定数组中读取或选择最后n个数字的三种不同方法。第二种和第三种解决方案基于向量,而不是第一种方法使用的静态默认数组。前两个问题的答案很简单。我们使用for循环逐个复制最后k个元素。最后一种技术是最简单的,它使用向量构造函数通过使用另一个向量的迭代器来复制组件来生成一个向量。
以上是获取数组中最后给定数量的项目的C++程序的详细内容。更多信息请关注PHP中文网其他相关文章!