自定义排序规则的multiset用法(1)
作者:互联网
#include<iostream> #include<cstring> #include<set> using namespace std; struct Rule1 { bool operator()(const int& a, const int& b)const { return (a % 10) < (b % 10); }//返回值为true则说明a必须排在b前面 }; int main() { multiset<int, greater<int>>st;//排序规则从大到小,如果默认排序方式则从小到大 int a[10]= { 1,23,45,67,44,44,77,88,2,3 }; for (int i = 0; i < 10; ++i) cout<<a[i]<<","; cout << endl; for (int i = 0; i < 10; ++i) st.insert(a[i]); multiset<int, greater<int>>::iterator i; for (i = st.begin(); i != st.end(); ++i) cout << *i << ","; cout << endl; multiset<int, Rule1>st2; for(int i=0;i<10;++i) st2.insert(a[i]); multiset<int, greater<int>>::iterator p; for (p = st2.begin(); p != st2.end(); ++p) cout << *p << ","; cout << endl; p = st2.find(133); cout << *p << endl;//发现第一个个位数是3的时候就跳出find函数了 return 0; }
标签:10,include,const,cout,自定义,int,st2,multiset,排序 来源: https://www.cnblogs.com/luyufan/p/15460070.html