板子

开始你的表演

1、设想要使用new建立一个rows行,cols列的矩阵:

int rows, cols ;
int **array = new int*[rows] ; 
for (int i = 0 ; i < rows ; i++) {
    array[i] = new int[cols] ; 
    memset(*array, 0, sizeof(int)*cols) ;    // memset()是按字节填充,值一般只为0或-1。
}  

2、结构体重载操作符模板:

Point operator + (const Point& A, const Point& B) {  
     return Point(A.x+B.x, A.y+B.y); 
}
ostream& operator << (ostream &out, const Point& p) {
    out << "(" << p.x << "," << p.y << ")";  
    return out; 
}
Point a, b;
cout << a+b << endl; 

3、求和函数模板sum():

template<typename T> T sum(T* begin, T* end) {  
    T *p = begin;  
    T ans = 0;  
    for(T *p = begin; p != end; p++)   
        ans = ans + *p;   
    return ans; 
}

4、结构体和类也可以是带模板的,如Point中x,y可以为int或double等多种类型:

template <typename T> struct Point {  
    T x, y;  
    Point(T x=0, T y=0) : x(x),y(y) { } 
}; 

5、重载转换运算符 :
对于类X的成员函数 X::operator T()
T是类型名 ,定义了一个X到T的转换。

operator bool() const {
   return p;
} 
Handle h; 
bool b=h;       //在这里调用  

6、C++中const 引用的是对象时只能访问该对象的const 函数,因为其他函数有可能会修改该对象的成员,编译器为了避免该类事情发生,会认为调用非const函数是错误的,error:…discards qualifiers 的意思就是缺少限定符。
因此:** 类似getter函数一般需要在后面加const限定符 **

7、求gcd最大公约数

  • 最快算法:
int gcd(int a, int b){
    while(b^=a^=b^=a%=b) ;
    return a;
}
  • 一般算法:
int gcd(int a,  int b)  {          // 要求
     return b ? gcd(b, a%b) : a ;
}

8、普通快速幂的模板:

int fastpow(int base, int n, int mod){
	int ans=1; 
	while(n){
		if(n&1) 
                    ans *= base % mod ;
		base *= base;
		n >>= 1;
	}
	return ans%mod;
}

9、快速读入整数( 远快于 scanf () 与 cin )

inline int read() {
    int x = 0, f = 1; char ch = getchar();
    while(ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar(); }
    while(ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

10、

11、

Final、一道大杂烩真香 输入输出模板

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxn 500005
#define maxm 200005
#define INF 1234567890
#define p 1000000007
template<class T>inline bool read(T &x)
{
    x=0;register char c=getchar();register bool f=0;
    while(!isdigit(c)){if(c==EOF)return false;f^=c=='-',c=getchar();}
    while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
    if(f)x=-x;
    return true;
}
template<class T>inline bool readd(T &x)
{
    register ll X=0;register double y=1.0;register char c=getchar();register bool f=0;
    while(!isdigit(c)){if(c==EOF)return false;f^=c=='-',c=getchar();}
    while(isdigit(c))X=(X<<3)+(X<<1)+(c^48),c=getchar();
    x=X;
    if(c!='.')return true;
    c=getchar();
    while(isdigit(c))x+=(y/=10)*(c^48),c=getchar();
    if(f)x=-x;
    return true;
}
template<class T>inline bool readc(T &x)
{
    register char c=getchar();
    while(c==' '||c=='\n'||c=='\r'||c=='\t')c=getchar();
    if(c==EOF)return false;
    x=c;
    return true;
}
template<class T>inline bool readc(T *x)
{
    register char c=getchar();
    while(c==' '||c=='\n'||c=='\r'||c=='\t')c=getchar();
    if(c==EOF)return false;
    while(c!=' '&&c!='\n'&&c!='\r'&&c!='\t'&&c!=EOF)*x++=c,c=getchar();
    *x=0;
    return true;
}
template<class T>inline bool reads(T &x)
{
    x="";register char c=getchar();
    while(c==' '||c=='\n'||c=='\r'||c=='\t')c=getchar();
    if(c==EOF)return false;
    while(c!=' '&&c!='\n'&&c!='\r'&&c!='\t'&&c!=EOF)x+=c,c=getchar();
    return true;
}
template<class T>inline void print(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)print(x/10);
    putchar(x%10^48);
}
template<class T>inline void printd(T x,ll y)
{
    static ll mul[]={1};
    for(register ll i=1;i<=18;i++)
        mul[i]=(mul[i-1]<<3)+(mul[i-1]<<1);
    if(x<-1e-12)putchar('-'),x=-x;
    x*=mul[y];
    register ll x1=(ll)round(x),x2=x1/mul[y],x3=x1-x2*mul[y];
    print(x2);
    if(y>0)
    {
        putchar('.');
        for(register ll i=1;i<y&&x3*mul[i]<mul[y];putchar('0'),i++);
        print(x3);
    }
}
template<class T>inline void printc(T x){putchar(x);}
template<class T>inline void printc(T *x){while(*x)putchar(*x++);}
template<class T>inline void prints(T x){for(register ll i=0;x[i]!='\0';i++)putchar(x[i]);}
template<class T>inline T maxd(T a,T b){if(b>a)return b;return a;}
template<class T>inline T mind(T a,T b){if(b<a)return b;return a;}
template<class T>inline T gcd(T a,T b){while(a^=b^=a^=b%=a);return b;}
template<class T>inline T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T>inline T absd(const T &a){if(a<0)return -a;return a;}
template<class T>inline void print(T x,char c){print(x),putchar(c);}
template<class T>inline void printd(T x,ll y,char c){printd(x,y),putchar(c);}
template<class T>inline void printc(T x,T c){printc(x),putchar(c);}
template<class T>inline void printc(T *x,T c){printc(x),putchar(c);}
template<class T>inline void prints(T x,char c){prints(x),putchar(c);}

mua~

posted @ 2021-03-13 01:04  awysl  阅读(99)  评论(0)    收藏  举报