求交集和并集

#include<bits/stdc++.h>

using namespace std;

void jiaoji(vector<int> a,vector<int>b)
{
    int alen = a.size();
    int blen = b.size();
	vector<int> res;
	if (a[0] > b[blen - 1] || b[0] > a[alen - 1])
		return;
	
	int i = 0,j = 0;
	while (i < alen &&j < blen) 
	{
		if (a[i]>b[j])
			j++;
		else if (a[i] < b[j])
			i++;
		else
		{
			res.push_back(a[i]);
			cout << a[i] << "  ";
			i++; j++;
		}
	}
	cout << endl;
}
void bingji(vector<int> a, vector<int> b){
    vector<int> ANS;
    set_union(a.begin(),a.end(),b.begin(),b.end(),inserter(ANS,ANS.begin()));
    for(auto it:ANS)
        cout<<it<<" ";
    cout<<endl;
}   
int main()
{
    vector<int> a = {70, 31, 32, 33, 34, 35, 23,24};
    vector<int> b = {71, 33,34,35, 23,24,25};
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    jiaoji(a, b);
    bingji(a, b);
    return 0;
}

 

 
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页