set是一种集合容器.-所以采用迭代器进行访问
作用:保证每个元素都是独一无二的;
应用:插入、遍历、删除、搜索;
eg:
#include<iostream> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<set> using namespace std; set<int>a; int main() { int n,x; while(cin>>n&&n) { //clear()清零
a.clear();
//insert()插入
for(int i=0;i<n;i++) { cin>>x; a.insert(x); } int len=a.size(); //iterator()顺序遍历 cout<<"len:"<<len<<endl; cout<<"顺序遍历"<<endl; set<int>::iterator s; for(s=a.begin();s!=a.end();s++) cout<<*s<<" "; cout<<endl; //reverse_iterator()反向遍历 cout<<"反向遍历"<<endl; set<int>::reverse_iterator t; for(t=a.rbegin();t!=a.rend();t++) cout<<*t<<" "; cout<<endl; //erase()删除 cout<<"please input the number that you want to delete:"<<endl; int tx; cin>>tx; a.erase(tx); set<int>::iterator s1; for(s1=a.begin();s1!=a.end();s1++) cout<<*s1<<" "; cout<<endl; //find()检索 cout<<"please input the number you want to find:"<<endl; cin>>tx; set<int>::iterator s2; s2=a.find(tx); if(s2!=a.end()) cout<<*s2<<endl; else cout<<"not find it!"<<endl; system("cls"); } }