1. 程式人生 > 其它 >自定義排序規則的multiset用法(1)

自定義排序規則的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; }