Skip to content

Instantly share code, notes, and snippets.

@rhizoome
Created April 23, 2018 09:59
Show Gist options
  • Save rhizoome/843863d29a2d9a48529e9a45abd841d8 to your computer and use it in GitHub Desktop.
Save rhizoome/843863d29a2d9a48529e9a45abd841d8 to your computer and use it in GitHub Desktop.
Cool cpp
#include <vector>
#include <numeric>
struct sum_f
{
template<class T, class U>
auto operator()(T x, U y) const
{
auto p = x * y;
return x + y + p;
}
};
constexpr auto sum = sum_f();
int main()
{
std::vector<int> v = { 1, 2, 3 };
int total = std::accumulate(v.begin(), v.end(), 0, sum);
std::vector<float> w = { 1, 2, 3 };
int ftotal = std::accumulate(w.begin(), w.end(), 0, sum);
std::vector<char> x = { 1, 2, 3 };
int xtotal = std::accumulate(x.begin(), x.end(), 0, sum);
return total + ftotal + xtotal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment