二元谓词

#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>

//二元谓词

class Mycmp
{
public:

    bool operator()(int val1, int val2)
    {
        return val1 > val2;
    }
};

int main(void)
{
    vector<int> v;
    v.push_back(10);
    v.push_back(40);
    v.push_back(30);
    v.push_back(60);
    v.push_back(20);

    //快排算法
    sort(v.begin(), v.end());

    for (vector<int>::iterator it = v.begin(); it < v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    sort(v.begin(), v.end(), Mycmp());//使用函数对象 改变算法策略 变为排序规则从大到小

    for (vector<int>::iterator it = v.begin(); it < v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    return 0;

}

 

posted @ 2021-01-16 12:17  loliconsk  阅读(138)  评论(0)    收藏  举报