首页 > 后端开发 > C++ > 正文

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

WBOY
发布: 2023-08-26 22:05:10
转载
1121人浏览过

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

数组是专门用于在一系列内存区域中保留同类型数据的数据结构。使用数组的主要好处是我们可以使用索引参数从任何位置访问它们。然而,插入和删除数据需要顺序操作,这将使得这个数据结构变成线性数据结构。我们可以简单地使用方括号中的索引或位置号来从数组中提取元素。本文将演示如何在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

    立即学习C++免费学习笔记(深入)”;

  • 如果 k

    • 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 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 );
}
登录后复制

Output

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的一部分,它是动态数组。让我们来看看代码。算法保持不变。

Example

#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 ); 
}
登录后复制

Output

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个元素。让我们看一下代码来理解这个概念。

Example

#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 ); 
}
登录后复制

Output

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个元素。

Conclusion

本文介绍了从给定数组中读取或选择最后n个数字的三种不同方法。第二种和第三种解决方案基于向量,而不是第一种方法使用的静态默认数组。前两个问题的答案很简单。我们使用for循环逐个复制最后k个元素。最后一种技术是最简单的,它使用向量构造函数通过使用另一个向量的迭代器来复制组件来生成一个向量。

以上就是获取数组中最后给定数量的项目的C++程序的详细内容,更多请关注php中文网其它相关文章!

c++速学教程(入门到精通)
c++速学教程(入门到精通)

c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号