排序之set集合排序
#include <iostream> #include <string> #include <set> #include <sstream> using namespace std; set<int> dict; int main() { int num; cout<<"输入一串数字,以0结束"<<endl; while (cin>>num,num) { dict.insert(num); } cout<<"排序之后"<<endl; for (set<int>::iterator it = dict.begin();it != dict.end();it++) { cout<<*it<<" "; } cout<<endl; return 0; }
升序排列
#include <iostream> //对于重复的数字将会忽略 #include <string> #include <set> #include <sstream> using namespace std; set<int> dict; int main() { int num,flag=1; cout<<"输入一串数字,以0结束"<<endl; while (cin>>num,num) { dict.insert(num); } cout<<"排序之后"<<endl; for (set<int>::iterator it = dict.end();it != dict.begin();it--) { if(flag) it--,flag=0; cout<<*it<<" "; } cout<<*(dict.begin()); cout<<endl; return 0; }
降序排列
也可以用反向迭代
#include <iostream> #include <string> #include <set> #include <sstream> using namespace std; set<int> dict; int main() { int num; cout<<"输入一串数字,以0结束"<<endl; while (cin>>num,num) { dict.insert(num); } cout<<"排序之后"<<endl; for (set<int>::reverse_iterator it = dict.rbegin();it != dict.rend();it++) { cout<<*it<<" "; } cout<<endl<<dict.size()<<endl;//验证重复元素不会插入 return 0; }或者自定义比较规则
1.元素不是结构体
#include <iostream> #include <string> #include <set> #include <sstream> using namespace std; struct mycom { bool operator () (const int &a,const int &b) //重载() { if (a != b) { return a>b; } else return a>b; } }; int main() { int num; set<int,mycom> dict; cout<<"输入一串数字,以0结束"<<endl; while (cin>>num,num) { dict.insert(num); } cout<<"排序之后"<<endl; for (set<int,mycom>::iterator it = dict.begin();it != dict.end();it++) { cout<<*it<<" "; } cout<<endl<<dict.size()<<endl;//验证重复元素不会插入 return 0; }2.元素是结构体
#include<iostream> #include<set> #include<string> using namespace std; struct Info { string name; double score; bool operator < (const Info &a) const // 重载“<”操作符,自定义排序规则 { //按score由大到小排序。如果要由小到大排序,使用“>”即可。 return a.score < score; } }; int main() { set<Info> s; Info info; //插入三个元素 info.name = "Jack"; info.score = 80; s.insert(info); info.name = "Tom"; info.score = 99; s.insert(info); info.name = "Steaven"; info.score = 60; s.insert(info); set<Info>::iterator it; for(it = s.begin(); it != s.end(); it++) cout << (*it).name << " : " << (*it).score << endl; return 0; }
删除与清空
#include <iostream> #include <string> #include <set> #include <sstream> using namespace std; set<int> dict; int main() { int num; cout<<"输入一串数字,以0结束"<<endl; while (cin>>num,num) { dict.insert(num); } cout<<"(升序)排序之后"<<endl; for (set<int>::iterator it = dict.begin();it != dict.end();it++) { cout<<*it<<" "; } cout<<endl; cout<<"需要删除的数字为 "; cin>>num; dict.erase(num); for (set<int>::reverse_iterator rit = dict.rbegin();rit != dict.rend();rit++) { cout<<*rit<<" "; } cout<<endl<<"清空之后 "; dict.clear(); cout<<endl<<dict.size()<<endl;//验证重复元素不会插入 return 0; }
查找
#include <iostream> #include <string> #include <set> #include <sstream> using namespace std; set<int> dict; int main() { int num; cout<<"输入一串数字,以0结束"<<endl; while (cin>>num,num) { dict.insert(num); } cout<<"(升序)排序之后"<<endl; for (set<int>::iterator it = dict.begin();it != dict.end();it++) { cout<<*it<<" "; } cout<<endl; cout<<"查找的数为 "; cin>>num; it = dict.find(num); if(it == dict.end()) cout<<"not found"; else cout<<*it; cout<<endl<<dict.size()<<endl;//验证重复元素不会插入 return 0; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。