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

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -