2018-10-15

2018-10-18  本文已影响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
上一篇下一篇

猜你喜欢

热点阅读