From e39b2a8ef22c22a8dcc10eeb7de81c26c5c4e957 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Sun, 20 Jan 2019 13:02:31 +0100 Subject: Update vector.cpp to use STL std::transform and std::inner_product --- vector/vector.cpp | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'vector') diff --git a/vector/vector.cpp b/vector/vector.cpp index 8057cdd..260f08f 100644 --- a/vector/vector.cpp +++ b/vector/vector.cpp @@ -5,6 +5,7 @@ #include #include +#include #include @@ -61,10 +62,12 @@ T basic_vec::length() const { template basic_vec operator+(const basic_vec& rhs, const basic_vec& lhs) { basic_vec out; - auto rit = rhs.cbegin(), lit = lhs.cbegin(); - std::generate(out.begin(), out.end(), [rit, lit] () mutable { - return *(rit++) + *(lit++); - }); + + std::transform(rhs.begin(), rhs.end(), lhs.begin(), out.begin(), + [](const T& r, const T& l) -> T { + return r + l; + } + ); return out; } @@ -72,10 +75,10 @@ basic_vec operator+(const basic_vec& rhs, const basic_vec& lhs template basic_vec operator*(const T& rhs, const basic_vec& lhs) { basic_vec out; - auto lit = lhs.cbegin(); - std::generate(out.begin(), out.end(), [rhs, lit] () mutable { - return rhs * *(lit++); + std::transform(lhs.begin(), lhs.end(), out.begin(), + [rhs](const T& t) -> T { + return t * rhs; }); return out; @@ -88,12 +91,7 @@ basic_vec operator-(const basic_vec& rhs, const basic_vec& lhs template T operator*(const basic_vec& rhs, const basic_vec& lhs) { - T res = basic_vec::null_element; - for (std::size_t i = 0; i < d; i++) { - res += rhs[i] * lhs[i]; - } - - return res; + return std::inner_product(rhs.begin(), rhs.end(), lhs.begin(), 0); } template -- cgit v1.2.1