1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
/* 4. Implement count_if() yourself! Test it !*/
#include <vector>
#include <iostream>
template<class Iter, class Pred>
int count_if(Iter begin, Iter end, Pred pred){
int sum = 0;
for(; begin != end; ++begin){
if(pred(*begin)) sum++;
}
return sum;
}
template<class T>
bool bigger_than(T val){
if(10 < val) return true;
else return false;
}
struct biger{
int val;
biger(int v) : val{v}{}
bool operator()(int V){
if(val < V) return true;
else return false;
}
};
int main(){
std::vector<int> vec {8, 12, 9, 5, 33, 4, 5,
4, 1, 20, 18, 17, 28, 1};
//std::cout << count_if(vec.begin(), vec.end(), biger(10)) << std::endl;
std::cout << count_if(vec.begin(), vec.end(), bigger_than) << std::endl;
}
|