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