STL--set_difference

时间:2024-11-27 10:35:39

set_difference(),作用是求两个集合的差。即求A-B(属于A但不属于B的元素)

set_difference()算法计算两个集合[start1, end1)和[start2, end2)的差集, 并将差集存放到result.

两个集合以序列的形式给出, 且必须先按升序排好位置.

set_difference()是一个指向result序列末尾的迭代器. 

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int a[],b[]; int main(){
int n,m;
while(scanf("%d%d",&n,&m))
{
if(n == && m == )
break;
int i;
for(i = ;i < n;i++)
scanf("%d",&a[i]);
for(i = ;i < m;i++)
scanf("%d",&b[i]);
sort(a,a + n);
sort(b,b + m);
vector<int> v();
vector<int>::iterator it;
it = set_difference(a,a + n,b,b + m,v.begin());
v.resize(it-v.begin());
if(v.size() == )
cout<<"NULL"<<endl;
else
{
for(it = v.begin(); it != v.end(); it++)
cout<<*it<<" ";
cout<<endl;
} }
return ;
}

STL--set_difference