美文网首页
3.二元谓词

3.二元谓词

作者: lxr_ | 来源:发表于2021-04-27 10:14 被阅读0次
#include<iostream>
using namespace std;

#include<vector>
#include<algorithm>

class MyCompare
{
public:
    bool operator()(int v1, int v2)
    {
        return v1 > v2;
    }
};
void test0301()
{
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }

    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(),MyCompare());//传入仿函数,

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

}

int main()
{

    test0301();
    system("pause");
    return 0;
}

相关文章

网友评论

      本文标题:3.二元谓词

      本文链接:https://www.haomeiwen.com/subject/lddjrltx.html