c++ - How to make function like operator<template>() form <functional> -
i have method:
#include <vector> #include <numeric> template <typename t> class mvector: public std::vector<t> { template<typename operation> t accumulate (operation op, t init = (t)0) { typename mvector<t>::const_iterator begin = this->begin(); typename mvector<t>::const_iterator end = this->end(); return std::accumulate(begin, end, init, op); } };
and can use pass example std::plus<int>
so:
#include <functional> v.accumulate(std::plus<int>());
my question how make own function able pass way. example:
v.accumulate(f<int>());
where f(x, y) = x+y-1
if want syntax, make f
functor template:
template <typename t> struct f { t operator() (const t& a, const t& b) const { return a+b-1; } };
otherwise, can use function template, in anton's answer.
Comments
Post a Comment