• 函数对象


    // template<typename _InputIterator, typename _Tp, typename _BianryOperation> 
    // _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BianryOperation __binary_op) {
    //     for (; __first != __last; ++__first)
    //         __init = __binary_op(__init, *__first);
    //     return __init;
    // }
    
    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<numeric>
    #include<functional>
    
    using namespace std;
    
    int sumSquares(int total, int value) {
        return total + value * value;
    }
    
    template<class T>
    void PrintInterval(T first, T last) {
        for (; first != last; ++first) {
            cout << *first << ",";
        }
        cout << endl;
    }
    
    template<class T>
    class SumPowers {
    private:
        int power;
    public:
        SumPowers(int p):power(p) {}
        const T operator()(const T& total, const T& value) {
            T v = value;
            for (int i = 0; i < power - 1; ++i)
                v = v * value;
            return total + v;
        }
    };
    
    int main() {
        const int SIZE = 10;
        int a1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        vector<int> v(a1, a1 + SIZE);
        cout << "1) "; PrintInterval(v.begin(), v.end());
        int result = accumulate(v.begin(), v.end(), 0, sumSquares);
        cout << "2) 平方和: " << result << endl;
        result = accumulate(v.begin(), v.end(), 0, SumPowers<int>(3));
        cout << "3) 立方和: " << result << endl;
        result = accumulate(v.begin(), v.end(), 0, SumPowers<int>(4));
        cout << "4) 四次方和: " << result << endl;
        return 0;
    }
    

      

    greater 的应用

    list 有两个sort成员函数

     void sort(); 将list中的元素按 “<” 规定的比较方法升序排列。

     template void sort (Compare op); 将list中的元素按 op 规定的比较方法升序排列。即要比较x,y 大小时,看 op(x,y)的返回值,为true则认为 x小于y

    // template<class T>
    // struct greater:public binary_function<T, T, bool> {
    //     bool operator()(const T& x, const T& y) const {
    //         return x > y;
    //     }
    // };
    
    #include<list>
    #include<iostream>
    using namespace std;
    
    class MyLess {
    public:
        bool operator()(const int& c1, const int& c2) {
            return (c1 % 10) < (c2 % 10);
        }
    };
    
    template<class T>
    void Print(T first, T last) {
        for (; first != last; ++first)
            cout << *first << ",";
    }
    
    int main() {
        const int SIZE = 5;
        int a[SIZE] = {5, 21, 14, 2, 3};
        list<int> lst(a, a+SIZE);
        lst.sort(MyLess());
        Print(lst.begin(), lst.end());
        cout << endl;
        lst.sort(greater<int>());
        Print(lst.begin(), lst.end());
        cout << endl;
        return 0;
    }
    

      

    学到这个地方发现以前很多不懂的东西现在好像都明白了,有一种醍醐灌顶的感觉。

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    UIScrollView
    xcode debug
    ios 开发小技巧
    xcode调试
    Objective-C的反射机制
    git 命令
    iOS block的一些理解
    iOS 开发常用宏
    iOS 常用函数
    转:支付宝系统架构(内部架构图)
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10335905.html
Copyright © 2020-2023  润新知