美文网首页
2018-10-15

2018-10-15

作者: 吃柠檬的鸮 | 来源:发表于2018-10-18 15:24 被阅读0次

    #include <iostream>

    #include <vector>

    using namespace std;

    template <typename T>

    struct LessThan : binary_function<T, T, T> {

        T operator()(const T& x, const T& y) {

        return x < y ? true : false;

    }

    };

    template <typename T>

    struct GreaterThan : binary_function<T, T, T> {

        T operator()(const T& x, const T& y) {

        return x > y ? true : false;

    }

    };

    template <typename iterType, typename elemType, typename binaryOperation>

    void fliter(iterType beg, iterType end, elemType fliter_value, vector<elemType> &v, binaryOperation binary_op) {

        while (beg != end) {

            if (binary_op(*beg, fliter_value)){

        v.push_back(*beg);

    }

    ++beg;

        }

    }

    template <typename iterT>

    void display(iterT beg, iterT end) {

        while(beg != end) {

        cout << *beg++ << "  ";

    }

    cout << endl;

    }

    int main() {

    float fa[6] = {6.3, 21.1, 13.4, 9.7, 18, 1.1};

    vector<int> iv = {12, 8, 3, 20, 4, 17};

    vector<float> frv;

    fliter(fa, fa + 6, (float)11, frv, LessThan<float>());

    cout << "fa[6] : " << endl;

    display(fa, fa + 6);

    cout << "Numbers less than 11 : " << endl;

    display(frv.begin(), frv.end());

    // for(auto it = frv.begin(); it != frv.end(); ++it) {

    //    cout << *it << "  ";

    // }

    // cout << endl;

    vector<int> irv;

    fliter(iv.begin(), iv.end(), 7, irv, GreaterThan<int>());

    cout << "vector iv : " << endl;

    display(iv.begin(), iv.end());

    cout << "Numbers greater than 7 : " << endl;

    display(irv.begin(), irv.end());

    return 0;

    }

    2018-10-15

    相关文章

      网友评论

          本文标题:2018-10-15

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