模板库

时间:2019-09-13
本文章向大家介绍模板库,主要包括模板库使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

基础模板

read()+print()

inline int read() {
    char ch;
    bool bj=0;
    while(!isdigit(ch=getchar()))
        bj|=(ch=='-');
    int res=ch^(3<<4);
    while(isdigit(ch=getchar()))
        res=(res<<1)+(res<<3)+(ch^(3<<4));
    return bj?-res:res;
}
void printnum(int x) {
    if(x>9)printnum(x/10);
    putchar(x%10+'0');
}
inline void print(int x,char ch) {
    if(x<0) {
        putchar('-');
        x=-x;
    }
    printnum(x);
    putchar(ch);
}

fread()

struct ios{
    inline char read(){
        static const int IN_LEN=1<<18|1;
        static char buf[IN_LEN],*s,*t;
        return (s==t)&&(t=(s=buf)+fread(buf,1,IN_LEN,stdin)),s==t?-1:*s++;
    }
    template <typename _Tp> inline ios & operator >> (_Tp&x){
        static char c11,boo;
        for(c11=read(),boo=0;!isdigit(c11);c11=read()){
            if(c11==-1)return *this;
            boo|=c11=='-';
        }
        for(x=0;isdigit(c11);c11=read())x=x*10+(c11^'0');
        boo&&(x=-x);
        return *this;
    }
}io;

高精度+重载运算符

const int MAX=100;
struct node {
    int num[MAX];
    node & operator = (const char*);
    node & operator = (int);
    node();
    node(int);
    bool operator > (const node &) const;
    bool operator < (const node &) const;
    bool operator <= (const node &) const;
    bool operator >= (const node &) const;
    bool operator != (const node &) const;
    bool operator == (const node &) const;
    node operator + (const node &) const;
    node operator - (const node &) const;
    node operator * (const node &) const;
    node operator / (const node &) const;
    node operator % (const node &) const;
    node & operator += (const node &);
    node & operator -= (const node &);
    node & operator *= (const node &);
    node & operator /= (const node &);
    node & operator %= (const node &);
};
node & node::operator = (const char* c) {
    memset(num,0,sizeof(num));
    int n=strlen(c),j=1,k=1;
    for (int i=1; i<=n; i++) {
        if (k==10000) j++,k=1;
        num[j]+=k*(c[n-i]-'0');
        k*=10;
    }
    num[0]=j;
    return *this;
}
node & node::operator = (int a) {
    char s[MAX];
    sprintf(s,"%d",a);
    return *this=s;
}
node::node() {
    memset(num,0,sizeof(num));
    num[0]=1;
}
node::node (int n) {
    *this = n;
}
bool node::operator > (const node &b) const {
    if (num[0]!=b.num[0]) return num[0]>b.num[0];
    for (int i=num[0]; i>=1; i--)
        if (num[i]!=b.num[i])
            return (num[i]>b.num[i]);
    return false;
}
bool node::operator < (const node &b) const {
    return b>*this;
}
bool node::operator <= (const node &b) const {
    return !(*this>b);
}

bool node::operator >= (const node &b) const {
    return !(b>*this);
}

bool node::operator != (const node &b) const {
    return (b>*this)||(*this>b);
}

bool node::operator == (const node &b) const {
    return !(b>*this)&&!(*this>b);
}
node node::operator + (const node &b) const {
    node c;
    c.num[0] = max(num[0], b.num[0]);
    for (int i=1; i<=c.num[0]; i++) {
        c.num[i]+=num[i]+b.num[i];
        if (c.num[i]>=10000) {
            c.num[i]-=10000;
            c.num[i+1]++;
        }
    }
    if (c.num[c.num[0]+1]>0) c.num[0]++;
    return c;
}
node node::operator - (const node &b) const {
    node c;
    c.num[0] = num[0];
    for (int i=1; i<=c.num[0]; i++) {
        c.num[i]+=num[i]-b.num[i];
        if (c.num[i]<0) {
            c.num[i]+=10000;
            c.num[i+1]--;
        }
    }
    while (c.num[c.num[0]]==0&&c.num[0]>1) c.num[0]--;
    return c;
}
node & node::operator += (const node &b) {
    return *this=*this+b;
}

node & node::operator -= (const node &b) {
    return *this=*this-b;
}
node node::operator * (const node &b) const {
    node c;
    c.num[0] = num[0]+b.num[0]+1;
    for (int i=1; i<=num[0]; i++) {
        for (int j=1; j<=b.num[0]; j++) {
            c.num[i+j-1]+=num[i]*b.num[j];
            c.num[i+j]+=c.num[i+j-1]/10000;
            c.num[i+j-1]%=10000;
        }
    }
    while (c.num[c.num[0]]==0&&c.num[0]>1) c.num[0]--;
    return c;
}
node & node::operator *= (const node &b) {
    return *this=*this*b;
}
node node::operator % (const node &b) const {
    node c, d;
    c.num[0] = num[0]+b.num[0]+1;
    d.num[0] = 0;
    for (int i=num[0]; i>=1; i--) {
        memmove(d.num+2, d.num+1, sizeof(d.num)-sizeof(int)*2);
        d.num[0]++;
        d.num[1]=num[i];
        int left=0, right=9999, mid;
        while (left < right) {
            mid = (left+right)/2;
            if (b*node(mid) <= d) left=mid+1;
            else right=mid;
        }
        c.num[i]=right-1;
        d=d-b*node(right-1);
    }
    while (c.num[c.num[0]]==0&&c.num[0]>1) c.num[0]--;
    return d;

}
node & node::operator /= (const node &b) {
    return *this=*this/b;
}

node & node::operator %= (const node &b) {
    return *this=*this%b;
}
node node::operator / (const node& b) const {
    node c, d;
    c.num[0] = num[0]+b.num[0]+1;
    d.num[0] = 0;
    for (int i=num[0]; i>=1; i--) {
        memmove(d.num+2, d.num+1, sizeof(d.num)-sizeof(int)*2);
        d.num[0]++;
        d.num[1]=num[i];
        int left=0, right=9999, mid;
        while (left < right) {
            mid = (left+right)/2;
            if (b*node(mid) <= d) left=mid+1;
            else right=mid;
        }
        c.num[i]=right-1;
        d=d-b*node(right-1);
    }
    while (c.num[c.num[0]]==0&&c.num[0]>1) c.num[0]--;
    return c;
}
ostream & operator << (ostream & o, node &n) {
    o<<n.num[n.num[0]];
    for (int i=n.num[0]-1; i>=1; i--) {
        o.width(4);
        o.fill('0');
        o<<n.num[i];
    }
    return o;
}
istream & operator >> (istream & in, node &n) {
    char s[MAX];
    in>>s;
    n=s;
    return in;
}

原文地址:https://www.cnblogs.com/soledadstar/p/11516568.html