C++程序:使用指定索引范围获取数组的子数组
数组是一种线性顺序数据结构,使用一系列内存段来存储同构数据。与其他数据结构类似,数组需要具备特定的特性,才能高效地插入、删除、遍历和更新元素。在C++中,我们的数组是静态的。C++中也提供了一些动态数组结构。在本文中,我们将了解如何在C++中使用起始和结束索引从更大的数组中获取子数组。
通过示例理解概念
Given array A = [10, 14, 65, 85, 96, 12, 35, 74, 69] Given two indices 2 and 7, it will pick elements [65, 85, 96, 12, 35] The picked elements are coming from index 2 to index 6, (excluding 7) of the given array A.
从示例中,我们可以看到给定了一个数组和两个索引,用于从这些位置中选取子数组。我们将看到不同模式下的解决方案。首先,使用传统的静态数组方法找到基本解决方案。让我们看看下面的算法以及C++实现代码。
算法
将数组A作为输入,并将两个索引i和j作为输入
创建一个名为Sub的空数组
对于索引k在范围i到j内,执行以下操作:
将A[k]插入到Sub中
结束循环
返回Sub
示例
#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 pickSubarray( int A[], int n, int sub[], int &m, int i, int j ) {
int ind = 0;
for( int k = i; k < j; k++, ind++ ) {
sub[ ind ] = A[ k ];
m += 1;
}
}
int main() {
int A[ Z ] = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
int n = 12;
int sub[ Z ];
int m = 0;
cout << "Given Array: ";
displayArr( A, n );
cout << "Sub array from index 3 to 9: ";
pickSubarray( A, n, sub, m, 3, 9);
displayArr( sub, m );
}
输出
Given Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, Sub array from index 3 to 9: 19, 86, 52, 32, 14, 76,
在这里,当我们将元素复制到新数组时,元素计数m也会逐一增加,以跟踪第二个数组或子数组中有多少个元素。
使用动态数组或向量
可以使用向量完成相同的事情。向量是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> pickSubarray( vector<int> A, int i, int j ) {
int ind = 0;
vector<int> sub;
for( int k = i; k < j; k++ ) {
sub.push_back( A[ k ] );
}
return sub;
}
int main() {
vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
vector<int> sub;
cout << "Given Array: ";
displayArr( A );
cout << "Sub array from index 3 to 9: ";
sub = pickSubarray( A, 3, 9);
displayArr( sub );
}
输出
Given Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, Sub array from index 3 to 9: 19, 86, 52, 32, 14, 76,
使用向量迭代器
之前的方法是一种手动过程。但是,我们可以使用向量迭代器完成相同的事情,在其中我们只需使用向量元素的起始和/或结束指针并添加偏移量(即索引)。它是直接使用优化的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> pickSubarray( vector<int> A, int i, int j ) {
auto first = A.begin() + i;
auto last = A.begin() + j;
vector<int> sub( first, last );
return sub;
}
int main() {
vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
vector<int> sub;
cout << "Given Array: ";
displayArr( A );
cout << "Sub array from index 3 to 9: ";
sub = pickSubarray( A, 3, 9);
displayArr( sub );
}
输出
Given Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, Sub array from index 3 to 9: 19, 86, 52, 32, 14, 76,
结论
在这里,我们看到了三种从给定数组创建子列表的不同方法。从数组和两个索引i和j(假设为起始和结束索引),形成另一个新数组。在第一种方法中,我们首先定义一个空数组,然后逐一将元素从起始索引复制到结束索引。第二种和第三种方法基于向量,向量是C++ STL中提供的动态数组。第二个解决方案显示了一种手动(用户定义)的元素从一个向量复制到最终向量的的 方法。使用for循环手动选取元素。在最后一种方法中,我们使用基于STL的方法来获得更快速和更简洁的解决方案。在该解决方案中,迭代器或指针用于直接从起始索引选取到结束索引的元素。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP