使用模板表示式的向量類

2021-03-31 08:56:58 字數 3417 閱讀 9738

// numeric.h

#ifndef _numeric_h_

#define _numeric_h_

#include

// memcpy

#include

// std::

using

namespace std;

namespace numeric

};template

class expr_sub

};template

class expr_mul

};template

class expr_div

};template

class tpl_expr

inline

double

operator(size_t i) const

};template

class tpl_expr

inline

double

operator(size_t i) const

};template

class tpl_expr

inline

double

operator(size_t i) const

};template

class tpl_expr double, op>

inline

double

operator(size_t i) const

};template

class tpl_expr int, op>

inline

double

operator(size_t i) const

};template

tpl_exprdouble> > operator+(const a& a, const b& b)

template

tpl_exprdouble> > operator-(const a& a, const b& b)

template

tpl_exprdouble> > operator*(const a& a, const b& b)

template

tpl_exprdouble> > operator/(const a& a, const b& b)//

class vector

vector(size_t n = 0, double d = 0.0)

: dim(n)

vector& operator=(const vector& v)

memcpy(ets, v.ets, sizeof(double)*dim);

}

return *this;

}

~vector()

public:

inline

double

operator(size_t i) const

inline

double& operator(size_t i)

public:

size_t get_dim() const

double* get_ptr() const

double sum_element() const

double min_element() const

}

return tmp;

}

double max_element() const

}

return tmp;

}

public:

vector& operator+()

vector& operator-()

public:

vector& operator+=(const vector& v)

vector& operator-=(const vector& v)

vector& operator*=(const vector& v)

vector& operator/=(const vector& v)

vector& operator+=(const

double& d)

vector& operator-=(const

double& d)

vector& operator*=(const

double& d)

vector& operator/=(const

double& d)

public:

template

vector& operator=(const tpl_expr& expr)

template

vector& operator+=(const tpl_expr& expr)

template

vector& operator-=(const tpl_expr& expr)

template

vector& operator*=(const tpl_expr& expr)

template

vector& operator/=(const tpl_expr& expr)

};};

#endif

// _numeric_h_

// test.cpp

#include

#include

#include

using

namespace std;

#include

"numeric.h"

using

namespace numeric;

void main()

time = clock() - time;

cout << z[0] << endl;

cout << time/1000.0

<< endl;

}

使用模板表示式的向量類

numeric.h ifndef numeric h define numeric h include memcpy include std using namespace std namespace numeric template class expr sub template class ex...

Lambda表示式和Stream類的使用

lambda表示式也叫閉包,允許把函式作為一個方法的引數,可以將 變得簡介緊湊,核心思想是將物件導向中的傳遞資料變成傳遞行為。對於只有單一方法的匿名類會顯得很繁瑣,lambda表示式可以讓我們直接將某種功能作為引數,可以讓上面的行為變得更加簡潔。關於lambda官方詳細文件戳這裡。1.基本語法為ex...

使用Lambda表示式重構模板方法

如果你需要採用某個演算法的框架,同時又希望有一定的靈活度,能對它的某些部分進行改進,那麼採用模板方法設計模式是比較通用的方案。好吧,這樣講聽起來有些抽象。換句話說,模板方法模式在你 希望使用這個演算法,但是需要對其中的某些行進行改進,才能達到希望的效果 時是非常有用的。public class on...

Mssql merge表示式的使用

基本語法格式 merge into target table 目標表 using source table 源表 on condition 匹配條件 when matched then sql語句 when not matched then sql語句 when not matched by sou...

java Lambda表示式的使用

如題,因為博主也是最近才接觸到lambda表示式的 ps 在這裡汗顏一會 我並不會講解它的原理,誠然任何一件事物如果理解原理的話,使用它必將更加容易。但博主在學習的時候,大多數時候都是學會怎麼用,然後在細究原理。就像你騎自行車之前,難道首先還要研究自行車的原理麼?首先lambda表示式的最簡單應用如...