set--insert的应用小事例

 小菜鸡报到,记录所学.......(望海涵!!!)

#include<cstdio>
#include<set>
#include<iostream>
using namespace std;
int main() {
	set<int>se;
	int n,m,i,k;
	while(scanf("%d %d",&n,&m)!=EOF) {
		for(i=0; i<n; i++) {
			scanf("%d",&k);
			se.insert(k);
		}
		for(i=0; i<m; i++) {
			scanf("%d",&k);
			se.insert(k);
		}
		for(set<int>::iterator i=se.begin();i!=se.end(); i++) 
		{
			if(i==se.begin ())
				cout<<*i;
			else 
			{
				cout<<" "<<*i;
			}
		}
		cout<<"\n";
		se.clear() ;

	}
	return 0;
}/*

Input
2 2    //输入 n、m 两个数
1 2     // n 个数
2 3    // m 个数

1 2    // 同上
1
1 2
Output
1 2 3    //第一组数据输出
1 2      //第二组数据输出

用 set 中的 insert 去掉重复的数
*/



猜你喜欢

转载自blog.csdn.net/sf_yang35/article/details/81149875