summaryrefslogtreecommitdiffstats
path: root/vector/vector.cpp
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2019-01-20 13:02:31 +0100
committerNao Pross <naopross@thearcway.org>2019-01-20 13:02:31 +0100
commite39b2a8ef22c22a8dcc10eeb7de81c26c5c4e957 (patch)
tree1c070260135949e0bfdbacca0cb83fef765dc4c3 /vector/vector.cpp
parentImplement vec2<T>::polar() former vec2<T>::angle() (diff)
downloadcplusplus-e39b2a8ef22c22a8dcc10eeb7de81c26c5c4e957.tar.gz
cplusplus-e39b2a8ef22c22a8dcc10eeb7de81c26c5c4e957.zip
Update vector.cpp to use STL std::transform and std::inner_product
Diffstat (limited to '')
-rw-r--r--vector/vector.cpp24
1 files changed, 11 insertions, 13 deletions
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 <array>
#include <algorithm>
+#include <numeric>
#include <initializer_list>
@@ -61,10 +62,12 @@ T basic_vec<T, d>::length() const {
template<typename T, std::size_t d>
basic_vec<T, d> operator+(const basic_vec<T, d>& rhs, const basic_vec<T, d>& lhs) {
basic_vec<T, d> 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<T, d> operator+(const basic_vec<T, d>& rhs, const basic_vec<T, d>& lhs
template<typename T, std::size_t d>
basic_vec<T, d> operator*(const T& rhs, const basic_vec<T, d>& lhs) {
basic_vec<T, d> 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<T, d> operator-(const basic_vec<T, d>& rhs, const basic_vec<T, d>& lhs
template<typename T, std::size_t d>
T operator*(const basic_vec<T, d>& rhs, const basic_vec<T, d>& lhs) {
- T res = basic_vec<T, d>::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<typename T, std::size_t d>