您好,欢迎访问一九零五行业门户网

C++程序从两个数组中查找公共元素

使用数组和数据结构可以在多个内存位置上存储同质(相同)数据。使用数组的主要优点是我们可以通过使用索引参数从任何地方访问它们。数据必须按顺序添加和删除的事实将这种数据结构转化为线性结构。要从数组中检索元素,我们只需要使用方括号内的索引或位置号码。在本文中,我们将使用c++获取两个数组中仅存在的共同元素。
理解概念并以示例说明given first array a = [10, 14, 65, 85, 96, 12, 35, 74, 69]given second array b = [23, 65, 89, 96, 12, 37, 71, 69]the common elements in arrays a and b are [65, 96, 12, 69]
在第一个数组中,有九个元素,在第二个数组中,有八个元素。所以这两个数组的大小可能不相同。我们的任务是找出这两个数组之间的共同元素。在这里,我们将看到一些解决这个问题的技巧。
天真的解决方案第一种也是最常见的解决方案是通过循环遍历第一个数组的每个元素,并对于第一个数组的每个条目在第二个数组中进行搜索。这种解决方案效率不高,但是更简单。让我们看一下算法和相应的实现。
算法将两个数组a和b作为输入
定义另一个数组 d 来保存所有重复的元素
对于a中的每个元素e1,执行以下操作
对于b中的每个元素e2,执行以下操作
如果 e1 = e2,则
将 e1 插入到 d 中
结束如果
结束循环
结束循环
返回 d
example 的中文翻译为:示例#include <iostream># define z 50using namespace std;void displayarr(int arr[], int n){ for( int i = 0; i < n; i++ ){ cout << arr[ i ] << , ; } cout << endl;}void findcommonelement( int a[], int n, int b[], int m, int d[], int &k ) { k = 0; for( int i = 0; i < n; i++ ) { for( int j = 0; j < m; j++ ) { if( a[ i ] == b[ j ] ) { d[ k ] = a[ i ]; k = k + 1; } } }}int main() { int a[ z ] = { 10, 14, 65, 85, 96, 12, 35, 74, 69 }; int n = 9; int b[ z ] = { 23, 65, 89, 96, 12, 37, 71, 69 }; int m = 8; int d[ z ]; int k = 0; cout << given first array a: ; displayarr( a, n ); cout << given second array b: ; displayarr( b, m ); findcommonelement( a, n, b, m, d, k ); cout << the common elements are: ; displayarr( d, k ); }
输出given first array a: 10, 14, 65, 85, 96, 12, 35, 74, 69, given second array b: 23, 65, 89, 96, 12, 37, 71, 69, the common elements are: 65, 96, 12, 69,
使用向量和set_intersection()函数使用c++ stl,set_intersection()函数返回公共元素作为迭代器对象。但是要使用此函数,我们必须将数组按升序排序。让我们来看看算法和c++实现代码。
算法将两个数组a和b作为输入
定义另一个数组 d 来保存所有重复的元素
为重复元素数组创建一个迭代器
使用 set_intersection() 方法将 a 和 b 数组进行交集运算,并将结果存储在 d 数组中
返回 d
example 的中文翻译为:示例#include <iostream>#include <algorithm>#include <vector># define z 50using namespace std;void displayarr( vector<int> v ){ for( int i = 0; i < v.size() ; i++ ){ cout << v[ i ] << , ; } cout << endl;}vector<int> findcommonelement( vector<int> a, vector<int> b ) { sort( a.begin(), a.end() ); sort( b.begin(), b.end() ); vector<int> duplicates; vector<int> d( a.size() + b.size() ); vector<int>::iterator dit, st; dit = set_intersection( a.begin(), a.end(), b.begin(), b.end(), d.begin() ); for( st = d.begin(); st != dit; ++st ) duplicates.push_back( *st ) ; return duplicates;}int main() { vector<int> a = { 10, 14, 65, 85, 96, 12, 35, 74, 69 }; vector<int> b = { 23, 65, 89, 96, 12, 37, 71, 69 }; vector<int> d; cout << given first array a: ; displayarr( a ); cout << given second array b: ; displayarr( b ); d = findcommonelement( a, b ); cout << the common elements are: ; displayarr( d ); }
输出given first array a: 10, 14, 65, 85, 96, 12, 35, 74, 69, given second array b: 23, 65, 89, 96, 12, 37, 71, 69, the common elements are: 12, 65, 69, 96,
结论在本文中,我们看到了两种从元素集合或两个数组中找到公共元素的方法。第一种朴素的解决方案是使用两个静态数组,通过逐个扫描每个元素来找到公共元素。这种解决方案的时间复杂度为o(n.m),其中n是第一个数组的大小,m是第二个数组的大小。下一种方法使用了基于c++ stl的set_intersection()方法。在这种方法中,我们需要使用排序后的向量。然后,该方法返回一个公共元素迭代器对象。我们可以从中创建一个向量并返回它。
以上就是c++程序从两个数组中查找公共元素的详细内容。
其它类似信息

推荐信息