Last active
May 31, 2019 21:14
-
-
Save FrBrGeorge/2903d9f34e520bfae0f325a3c0004b4e to your computer and use it in GitHub Desktop.
c++ vector with a bit of <functional>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
#include <functional> | |
#include <numeric> | |
using namespace std; | |
template <typename T> | |
class vect: public vector<T> { | |
public: | |
using vector<T>::vector; | |
operator string() const { | |
string res; | |
for(T e:*this) | |
res += (res[0]?" ":"<")+to_string(e); | |
return res+">"; | |
} | |
vect unary(function<T(T)> op) const { | |
vect<T> res(this->size()); | |
transform(this->cbegin(), this->cend(), res.begin(), op); | |
return res; | |
} | |
vect binary(const vect<T>& v, function<T(T, T)> op) const { | |
vect<T> res(this->size()); | |
transform(this->cbegin(), this->cend(), v.cbegin(), res.begin(), op); | |
return res; | |
} | |
vect operator+() const { | |
return unary([](T n) -> T { return n; }); | |
} | |
vect operator-() const { | |
return unary([](T n) -> T { return -n; }); | |
} | |
vect operator-(T b) const { | |
return unary([&b](T n) -> T { return n-b; }); | |
} | |
vect operator+(T b) const { | |
return unary([&b](T n) -> T { return n+b; }); | |
} | |
vect operator*(T b) const { | |
return unary([&b](T n) -> T { return n*b; }); | |
} | |
vect operator/(T b) const { | |
return unary([&b](T n) -> T { return n/b; }); | |
} | |
vect operator-(const vect<T>& v) const { | |
return binary(v, [](T a, T b) -> T { return a-b; }); | |
} | |
vect operator+(const vect<T>& v) const { | |
return binary(v, [](T a, T b) -> T { return a+b; }); | |
} | |
vect operator*(const vect<T>& v) const { | |
return binary(v, [](T a, T b) -> T { return a*b; }); | |
} | |
T operator|(const vect<T>& v) const { | |
vect<T> res = *this*v; | |
return accumulate(res.cbegin(), res.cend(), 0, plus<T>()); | |
} | |
vect<T> operator&(const vect<T>& v) const { | |
return vect<T>({(*this)[2]*v[1]-(*this)[1]*v[2], (*this)[0]*v[2]-(*this)[2]*v[0], (*this)[1]*v[0]-(*this)[0]*v[1]}); | |
} | |
T mixed(const vect<T>& a, const vect<T>& b) const { | |
return (*this)|(a&b); | |
} | |
}; | |
template <typename T> | |
ostream &operator << (ostream &out, vect<T> v) { | |
return out << (string)v; | |
} | |
template <typename T> | |
istream &operator >> (istream &in, vect<T> &v) { | |
T a; | |
v.clear(); | |
while(in.peek()!='\n') { | |
in >> a; | |
v.push_back(a); | |
} | |
return in; | |
} | |
int main() { | |
vect<int> v; | |
cin >> v; | |
cout << v << endl; | |
cout << vect<int>({1,2,3,4,5}) << endl; | |
cout << -vect<int>({1,2,3,4,5}) << endl; | |
cout << +vect<int>({1,2,3,4,5}) << endl; | |
cout << (vect<int>({1,2,3,4,5})+4+5) << endl; | |
cout << (vect<int>({1,2,3,4,5})-4) << endl; | |
cout << (vect<int>({1,2,3,4,5})+vect<int>({8,6,4,2,0})+vect<int>({3,8,32,6,4})) << endl; | |
cout << (vect<int>({1,2,3,4,5})-vect<int>({8,6,4,2,0})) << endl; | |
cout << (vect<int>({1,2,3,4,5})*vect<int>({8,6,4,2,0})) << endl; | |
cout << (vect<int>({1,2,3,4,5})|vect<int>({8,6,4,2,0})) << endl; | |
cout << (vect<int>({1,2,3})&vect<int>({8,6,4})) << endl; | |
cout << vect<int>({1,2,3}).mixed(vect<int>({8,6,4}), vect<int>({6,0,2})) << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment