#include <algorithm>
#include <cassert>
#include <functional>
#include <numeric>
#include <vector>
//From http://www.richelbilderbeek.nl/CppFunctorGreaterThan.htm
struct GreaterThan : public std::binary_function<int,int,int>
{
explicit GreaterThan(const int any_x = 0) : x(any_x) {}
int operator()(const int sum, const int y) const
{
return sum + (y <= x ? 0 : y);
}
private:
int x;
};
const std::vector<int> CreateVector()
{
std::vector<int> v;
v.push_back(-1);
v.push_back(0);
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
return v;
}
int main()
{
const std::vector<int> v = CreateVector();
assert(std::accumulate(v.begin(),v.end(),0,GreaterThan(5))==0);
assert(std::accumulate(v.begin(),v.end(),0,GreaterThan(4))==0);
assert(std::accumulate(v.begin(),v.end(),0,GreaterThan(3))==4);
assert(std::accumulate(v.begin(),v.end(),0,GreaterThan(2))==7);
assert(std::accumulate(v.begin(),v.end(),0,GreaterThan(1))==9);
assert(std::accumulate(v.begin(),v.end(),0,GreaterThan(0))==10);
}
|