#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
using namespace std;
void sort_algorithm_test()
{
cout<<"利用algorithm库中的排序算法测试:";
int a[4]={1,5,6,3};
sort(a,a+4);
for(int i=0;i<4;i++){
cout<<a[i]<<' ';
}
cout<<endl;
}
void display_set(set<int> &s)
{
for(set<int>::iterator it=s.begin();it!=s.end();it++)
cout<<*it<<" ";
cout<<endl;
//cout<<"1"<<endl;
}
void display_vector(vector<int> &s)
{
for(vector<int>::iterator it=s.begin();it!=s.end();it++)
cout<<*it<<" ";
cout<<endl;
//cout<<"1"<<endl;
}
int main()
{
cout<<"***************************************"<<endl;
sort_algorithm_test();
cout<<"***************************************"<<endl;
//set theory
//构建两个集合
//A={2,4,6} B={1,2,3,4,5}
set<int> a,b;
a.insert(2);
a.insert(4);
a.insert(6);
b.insert(1);
b.insert(2);
b.insert(3);
b.insert(4);
b.insert(5);
//打印集合a,b
cout<<"***************************************"<<endl;
cout<<"集合A包含的元素:";
display_set(a);
cout<<"集合A包含的元素:";
display_set(b);
cout<<"***************************************"<<endl;
//取并集AUB
cout<<"***************************************"<<endl;
cout<<"集合A与B取并集的结果:";
vector<int> rs1;
set_union(a.begin(),a.end(),b.begin(),b.end(),back_inserter(rs1));
display_vector(rs1);
cout<<"***************************************"<<endl;
//取交集A^B
cout<<"集合A与B取交集的结果:";
vector<int> rs2;
set_intersection(a.begin(),a.end(),b.begin(),b.end(),back_inserter(rs2));
display_vector(rs2);
cout<<"***************************************"<<endl;
//取差集 A-B
cout<<"集合A与B取差集的结果:";
vector<int> rs3;
set_difference(a.begin(),a.end(),b.begin(),b.end(),back_inserter(rs3));
display_vector(rs3);
cout<<"***************************************"<<endl;
cout<<"所有操作全部结束!"<<endl;
cout<<"***************************************"<<endl;
return 0;
}
C++ STL Set Theory的基本操作
最新推荐文章于 2024-11-14 19:11:37 发布