數運算的一些模板

2021-07-22 23:06:57 字數 2943 閱讀 7605

用過的一些模板

1.分數類的運算

#include 

#include

using

namespace

std;

using

namespace rel_ops;

template

long> class frac

frac(const val& x, const val& y = 1)

bool

operator

<(const frac& rhs) const

frac operator +(const frac& rhs) const

frac operator -(const frac& rhs) const

frac operator *(const frac& rhs) const

frac operator /(const frac& rhs) const

frac operator -() const

frac& operator +=(const frac& rhs)

frac& operator -=(const frac& rhs)

frac& operator *=(const frac& rhs)

frac& operator /=(const frac& rhs)

};

2.取膜運算

class imp ;    // 以下所有函式的傳入引數都必須在[0,mod)之間

imp(int v):u(v){} // 且運算子左側型別必須是imp

operator

int&()

operator

const

int&() const

imp operator +(int v) const

imp operator /(int v) const

imp operator -() const

imp& operator +=(int v)

imp& operator -=(int v)

imp& operator *=(int v)

imp& operator /=(int v)

static ll rr(int x)

};

3.大數運算

#include 

#include

#include

using

namespace

std;

class bignum

bignum(const

string& s)

}if(!n || cnt>=10) u[n++]=num;

if(!u[0] && n==1) o=0;

}bignum(long

long x)

operator

string() const

intoperator (int pos) const ;

for(static

int i=1;i1]*10;

return u[pos/bit]/e[pos%bit]%10;

}int length() const

friend

int cmp(const bignum& l, const bignum& r)

// 運算子

bool

operator

< (const bignum& r) const

bool

operator > (const bignum& r) const

bool

operator

<=(const bignum& r) const

bool

operator >=(const bignum& r) const

bool

operator ==(const bignum& r) const

bool

operator !=(const bignum& r) const

bignum operator +(const bignum& r) const

bignum operator -(const bignum& r) const

bignum operator *(int x) const

bignum operator /(int x) const

bignum& operator *=(const bignum& r)

bignum& operator /=(const bignum& r)

bignum& operator %=(const bignum& r)

bignum& operator %=(int x)

bignum operator -() const

bignum& operator +=(const bignum& r)

bignum& operator -=(const bignum& r)

bignum operator *(const bignum& r) const

return s;

}bignum operator /(const bignum& r) const

bignum operator %(const bignum& r) const

r.o^=ro,o^=lo;

if(s.u[0] || s.n>=2) s.o=o;

return s;

}bignum& operator *=(int x)

if(!u[0] && n==1) o=0;

return *this;

}bignum& operator /=(int x)

intoperator %(int x) const

};

c 模板的一些

stl中大量的運用了模板 今天就拿stl 的容器和一些相關的操作來說明下 std set 新增自定義比較操作 templatestruct lesscmp std set ms 2 迴圈遍歷,這個肯定木有難度了 template void myprint const t lhs for each m...

一些實用的模板

1 floyd判最小環 注 包含基本的floyd演算法。最好寫的單源最短路徑演算法。但時間複雜度o n 3 n 1000基本就用不了了。void floyd 3 dijkstra 迪傑斯特拉 演算法 單源最短路徑演算法 基本思路 每次確定到起點距離最短且距離尚未確定的點的dis,並對其所有鄰邊進行鬆...

一些模板的應用

首先,是曾經的講師lh大佬的模板 主要涉及了常用的檔案頭以及一些常用語句和函式的縮寫 include include include include include include include include include include using namespace std typedef...