C++程式檢查給定專案是否包含在陣列中
陣列是一種線性順序資料結構,用於在連續的記憶體位置儲存同構資料。與其他資料結構一樣,陣列也必須具備以某種有效方式插入、刪除、遍歷和更新元素的功能。在 C++ 中,我們的陣列是靜態的。C++ 中也有一些動態陣列結構可用。對於靜態陣列,可以儲存 Z 個元素。到目前為止,我們已經儲存了 n 個元素。在本文中,我們將學習如何使用 C++ 檢查元素是否存在於陣列中。
透過示例理解概念
Given array A = [10, 14, 65, 85, 96, 12, 35, 74, 69] Check whether 96 is present inside A or not. Yes, it is present, so return True.
在給定的示例中,我們有一個包含九個元素的陣列 A。我們將檢查元素 96 是否存在於 A 中。如果存在,則返回 true,否則對於不存在的元素返回 false。要檢查元素是否在陣列中,我們可以執行搜尋。對於未排序的給定陣列,我們需要執行線性搜尋。讓我們看看演算法以更好地理解。
演算法
獲取一個數組 A 和一個元素 k,以檢查 e 是否在 A 中
對於 A 中的每個元素 e,執行:
如果 e 與 k 相同,則
返回 true
結束 if
結束 for
返回 false
示例
#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;
}
string isInsideArray( int A[], int n, int k ){
for( int i = 0; i < n; i++) {
if( A [ i ] == k ) {
return "Yes";
}
}
return "No";
}
int main() {
int arr[ Z ] = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
int n = 12;
cout << "Array elements: ";
displayArr( arr, n );
cout << "Is 86 inside the array? :";
cout << isInsideArray( arr, n, 86 );
cout << "\nIs 900 inside the array? :";
cout << isInsideArray( arr, n, 900 );
cout << "\nIs 65 inside the array? :";
cout << isInsideArray( arr, n, 65 );
}
輸出
Array elements: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, Is 86 inside the array? :Yes Is 900 inside the array? :No Is 65 inside the array? :Yes
在 C++ STL 中使用向量
在上面的示例中,我們使用了靜態陣列,我們需要執行線性或順序搜尋來檢查元素是否存在於陣列中。在本節中,我們將使用來自 C++ STL 的向量。向量是動態資料結構,其中包含許多不同的函式。要搜尋向量中的元素,我們可以使用 find() 方法。此方法包含在“algorithm”標頭檔案中。讓我們看看它的程式碼。
示例
#include <iostream>
#include <vector>
#include <algorithm>
# define Z 50
using namespace std;
void displayArr( vector<int> v ){
for( int i = 0; i < v.size() ; i++ ){
cout << v[ i ] << ", ";
}
cout << endl;
}
string isInsideArray( vector<int> A, int k ){
if( find( A.begin(), A.end(), k ) != A.end() ) {
return "Yes";
}
return "No";
}
int main() {
vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
cout << "Array elements: ";
displayArr( A );
cout << "Is 86 inside the array? :";
cout << isInsideArray( A, 86 );
cout << "\nIs 900 inside the array? :";
cout << isInsideArray( A, 900 );
cout << "\nIs 65 inside the array? :";
cout << isInsideArray( A, 65 );
}
輸出
Array elements: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, Is 86 inside the array? :Yes Is 900 inside the array? :No Is 65 inside the array? :Yes
除了 find() 之外,我們還可以使用另一種方法來檢查元素是否存在於向量中,即向量物件的預設 count() 函式。當計數為 0 時,表示元素不存在,否則表示元素存在。實現如下所示
示例
#include <iostream>
#include <vector>
#include <algorithm>
# define Z 50
using namespace std;
void displayArr( vector<int> v ){
for( int i = 0; i < v.size() ; i++ ){
cout << v[ i ] << ", ";
}
cout << endl;
}
string isInsideArray( vector<int> A, int k ){
if( count( A.begin(), A.end(), k ) == 0 ) {
return "No";
}
return "Yes";
}
int main() {
vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
cout << "Array elements: ";
displayArr( A );
cout << "Is 86 inside the array? :";
cout << isInsideArray( A, 86 );
cout << "\nIs 900 inside the array? :";
cout << isInsideArray( A, 900 );
cout << "\nIs 65 inside the array? :";
cout << isInsideArray( A, 65 );
}
輸出
Array elements: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, Is 86 inside the array? :Yes Is 900 inside the array? :No Is 65 inside the array? :Yes
結論
在本文中,我們學習瞭如何檢查元素是否存在於陣列中。我們使用了靜態陣列和向量。對於靜態陣列,我們使用線性搜尋方法搜尋陣列。由於我們認為陣列中的元素未排序,因此使用線性搜尋。否則,我們可以對排序資料使用二分搜尋。在第二種和第三種方法中,我們使用向量,並使用 find() 和 count 方法來檢查元素是否存在於向量中。可能還有一些其他方法可以使用 STL 使用集合或 set_intersection() 來實現相同的功能。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP