38-【什么叫规矩 什么叫体统】内建函数

/*STL 内建了一些仿函数 内建函数对象
【分类】
1-算数仿函数
2-关系仿函数
3-逻辑仿函数

【算数仿函数】
template T plus         //加法仿函数
template T minus        //减法仿函数
template T multiplies   //乘法仿函数
template T divides      //除法仿函数
template T modulus      //取模仿函数
template T negate       //取反仿函数
...


【关系仿函数】
template bool equal_to         //等于
template bool not_equal_to     //不等于
template bool greater          //大于
template bool greater_equal    //大于等于
template bool less             //小于
template bool less_equal       //小于等于

【逻辑仿函数】
template bool logical_and       //与
template bool logical_or        //或
template bool logical_not       //非
*/

#include
#include
#include
#include
#include
using namespace std;
class MyCompare;
class MyCompare
{
public:
    bool operator()(int a,int b)
    {
        return a>b;
    }
};

int main()
{
    cout << "------------------------- 算数仿函数" << endl;
    //整型取反
    negate<int> n;
    cout << n(34) << endl;

    //加法
    plus<int> m;
    cout << m(123,123) <<endl;

    cout << "\n\n------------------------- 关系仿函数" << endl;
    vector<int> v;
    v.push_back(13);
    v.push_back(23);
    v.push_back(53);
    v.push_back(33);
    v.push_back(1);
    for (vector<int>::iterator it = v.begin();it != v.end();it++)
    {
        cout << *it << "  ";
    }
    cout << endl;
    //降序
    //sort(v.begin(),v.end(),MyCompare());
    sort(v.begin(),v.end(),greater<int>());
    for (vector<int>::iterator it = v.begin();it != v.end();it++)
    {
        cout << *it << "  ";
    }
    cout << endl;
    cout << "\n\n------------------------- 逻辑仿函数" << endl;

    vector<bool> v2;
    v2.push_back(true);
    v2.push_back(true);
    v2.push_back(true);
    v2.push_back(false);
    for (vector<bool>::iterator it = v2.begin();it != v2.end();it++)
    {
        cout << *it << "  ";
    }
    cout << endl;

    //利用逻辑非将容器v2复制到->v3
    vector<bool> v3;
    v3.resize(v2.size());
    transform(v2.begin(),v2.end(),v3.begin(),logical_not<bool>());//搬运函数
    for (vector<bool>::iterator it = v3.begin();it != v3.end();it++)
    {
        cout << *it << "  ";
    }
    cout << endl;

}


38-【什么叫规矩 什么叫体统】内建函数_第1张图片

你可能感兴趣的:(c++)