c++求两个数组的并集
时间: 2023-12-24 11:24:44 浏览: 27
可以使用STL中的set容器来求两个数组的并集。
示例代码如下:
```c++
#include <iostream>
#include <set>
using namespace std;
int main()
{
int a[] = {1, 2, 3, 4};
int b[] = {3, 4, 5, 6};
set<int> result;
for(int i = 0; i < 4; i++)
{
result.insert(a[i]);
result.insert(b[i]);
}
for(auto i : result)
{
cout << i << " ";
}
cout << endl;
return 0;
}
```
输出结果为:1 2 3 4 5 6
相关问题
c++求两个数组的并集和交集,能实现多组数据的多次运算,直到选择结束运算为止,提供问答界面,以便人机交互
下面是一个示例代码,实现了多组数据的多次运算,直到选择结束运算为止,并提供了问答界面,以便人机交互:
```c++
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i) {
cout << arr[i] << " ";
}
cout << endl;
}
void printSet(set<int> s)
{
for (auto it = s.begin(); it != s.end(); ++it) {
cout << *it << " ";
}
cout << endl;
}
void printIntersectionUnion(int arr1[], int n1, int arr2[], int n2)
{
set<int> set1(arr1, arr1 + n1);
set<int> set2(arr2, arr2 + n2);
vector<int> intersection(n1 + n2);
auto it = set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(), intersection.begin());
intersection.resize(it - intersection.begin());
vector<int> uni(n1 + n2);
it = set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), uni.begin());
uni.resize(it - uni.begin());
cout << "Intersection: ";
printArray(intersection.data(), intersection.size());
cout << "Union: ";
printSet(set<int>(uni.begin(), uni.end()));
}
int main()
{
int arr1[100], arr2[100], n1, n2;
char choice;
do {
cout << "Please enter the size of array 1: ";
cin >> n1;
cout << "Please enter the elements of array 1: ";
for (int i = 0; i < n1; ++i) {
cin >> arr1[i];
}
cout << "Please enter the size of array 2: ";
cin >> n2;
cout << "Please enter the elements of array 2: ";
for (int i = 0; i < n2; ++i) {
cin >> arr2[i];
}
printIntersectionUnion(arr1, n1, arr2, n2);
cout << "Do you want to continue? (y/n): ";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
cout << "Program ended." << endl;
return 0;
}
```
示例输出:
```
Please enter the size of array 1: 5
Please enter the elements of array 1: 1 2 3 4 5
Please enter the size of array 2: 5
Please enter the elements of array 2: 3 4 5 6 7
Intersection: 3 4 5
Union: 1 2 3 4 5 6 7
Do you want to continue? (y/n): y
Please enter the size of array 1: 3
Please enter the elements of array 1: 1 2 3
Please enter the size of array 2: 4
Please enter the elements of array 2: 2 3 4 5
Intersection: 2 3
Union: 1 2 3 4 5
Do you want to continue? (y/n): n
Program ended.
```
c++数组求并集
假设有两个数组A和B,它们的长度分别为n和m,可以使用以下代码求它们的并集:
```c++
#include <iostream>
using namespace std;
const int MAXN = 100;
int main() {
int n, m;
int A[MAXN], B[MAXN], C[MAXN]; // 定义三个数组
cin >> n; // 输入数组A的长度
for (int i = 0; i < n; i++) {
cin >> A[i]; // 输入数组A的元素
}
cin >> m; // 输入数组B的长度
for (int i = 0; i < m; i++) {
cin >> B[i]; // 输入数组B的元素
}
int k = 0; // 计数器
for (int i = 0; i < n; i++) { // 遍历数组A
bool flag = true; // 定义一个标志
for (int j = 0; j < m; j++) { // 遍历数组B
if (A[i] == B[j]) { // 如果A[i]等于B[j]
flag = false; // 标志置为false
break; // 跳出循环
}
}
if (flag) { // 如果标志为true
C[k++] = A[i]; // 将A[i]添加到C中,并将计数器加1
}
}
for (int i = 0; i < m; i++) { // 遍历数组B
bool flag = true; // 定义一个标志
for (int j = 0; j < n; j++) { // 遍历数组A
if (B[i] == A[j]) { // 如果B[i]等于A[j]
flag = false; // 标志置为false
break; // 跳出循环
}
}
if (flag) { // 如果标志为true
C[k++] = B[i]; // 将B[i]添加到C中,并将计数器加1
}
}
cout << "A和B的并集为:";
for (int i = 0; i < k; i++) { // 遍历数组C
cout << C[i] << " "; // 输出数组C的元素
}
cout << endl;
return 0;
}
```
该程序的思路是:遍历数组A,对于每个元素,如果它在数组B中出现过,则跳过;否则将它添加到数组C中。然后遍历数组B,对于每个元素,如果它在数组A中出现过,则跳过;否则将它添加到数组C中。最后输出数组C即可得到两个数组的并集。
阅读全文