Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <functional>
- #include <limits>
- #include <string>
- #include <cassert>
- #include <numeric>
- #include <vector>
- template<typename T, typename Func, typename LogFunc>
- class InstanceClassifier
- {
- std::vector<std::function<bool(const T&)>> functions;
- LogFunc log_func;
- public:
- InstanceClassifier()
- {}
- InstanceClassifier(std::vector<std::function<bool(const T&)>> functions_):
- functions(functions_)
- {}
- InstanceClassifier& operator = (const InstanceClassifier& other)
- {
- functions = other.functions;
- return *this;
- }
- bool isSatisfied(const T& t) const
- {
- if (log_func)
- {
- std::cout << ":)\n";
- log_func(t);
- }
- else
- {
- std::cout << ":(\n";
- }
- assert(functions.size());// pointless to call without functions
- if (!functions.size())
- {
- return true; //logic: all (all == 0 in this case :)) satisfy
- }
- std::vector<bool> results;// if editing remember remember vector<bool> is not a container
- for (auto it = functions.cbegin(); it!=functions.cend(); ++it)
- results.push_back( (*it) (t) );
- return std::accumulate(results.cbegin(), results.cend(), true, std::multiplies<bool>());
- }
- };
- int main()
- {
- std::function<void (int)> lambdada= [](int i){std::cout << i <<std::endl;};
- InstanceClassifier<int, std::plus<bool>, decltype (lambdada) > even_positive( {[](int i){return i%2==0;}, [](int i){return i>0;} });
- even_positive.isSatisfied(42);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement