c++求两个数组的并集
时间: 2023-12-24 12:13:23 浏览: 116
可以使用 STL 中的 set 容器来实现两个数组的并集。具体实现步骤如下:
1. 定义两个数组,并将它们存储到 set 容器中。
2. 创建一个新的 set 容器,遍历两个数组的 set 容器,将其中的元素插入到新的 set 容器中。
3. 遍历新的 set 容器,将其中的元素输出即为两个数组的并集。
下面是示例代码:
```c++
#include <iostream>
#include <set>
using namespace std;
int main()
{
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {4, 5, 6, 7, 8};
set<int> s1(arr1, arr1 + sizeof(arr1) / sizeof(int));
set<int> s2(arr2, arr2 + sizeof(arr2) / sizeof(int));
set<int> s3;
set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(s3, s3.begin()));
for (auto it = s3.begin(); it != s3.end(); it++)
cout << *it << " ";
return 0;
}
```
输出结果为:1 2 3 4 5 6 7 8。
相关问题
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即可得到两个数组的并集。
阅读全文