- Examples
Let's have a look at some examples of expression templates.
Example 1: Vector Arithmetic Based on Object-Orientation
Press + to interact
// vectorArithmeticOperatorOverloading.cpp#include <iostream>#include <vector>template<typename T>class MyVector{std::vector<T> cont;public:// MyVector with initial sizeexplicit MyVector(const std::size_t n) : cont(n){}// MyVector with initial size and valueMyVector(const std::size_t n, const double initialValue) : cont(n, initialValue){}// size of underlying containerstd::size_t size() const{return cont.size();}// index operatorsT operator[](const std::size_t i) const{return cont[i];}T& operator[](const std::size_t i){return cont[i];}};// function template for the + operatortemplate<typename T>MyVector<T> operator+ (const MyVector<T>& a, const MyVector<T>& b){MyVector<T> result(a.size());for (std::size_t s= 0; s <= a.size(); ++s){result[s]= a[s]+b[s];}return result;}// function template for the * operatortemplate<typename T>MyVector<T> operator* (const MyVector<T>& a, const MyVector<T>& b){MyVector<T> result(a.size());for (std::size_t s= 0; s <= a.size(); ++s){result[s]= a[s]*b[s];}return result;}// function template for << operatortemplate<typename T>std::ostream& operator<<(std::ostream& os, const MyVector<T>& cont){os << std::endl;for (std::size_t i=0; i<cont.size(); ++i) {os << cont[i] << ' ';}return os;}int main(){MyVector<double> x(10,5.4);MyVector<double> y(10,10.3);MyVector<double> result(10);result= x+x + y*y;std::cout << result << std::endl;}
Explanation
MyVector
is a simple wrapper for ...
Access this course and 1400+ top-rated courses and projects.