可以用于多变量多类型的C++快读快写模板

快读-输入一个

typedef long long ll;
template<typename T> void read(T &x)
{
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}

Example 1

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
template<typename T> void read(T &x)
{
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
int n;
int main()
{
    read(n);
    cout<<n;
    return 0;
}

快读-输入多个

typedef long long ll;
template<typename T> void read(T &x)
{
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) 
{
    read(first);
    read(args...);
}

Example 2

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
template<typename T> void read(T &x)
{
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) 
{
    read(first);
    read(args...);
}
int n,m;
int main()
{
    read(n,m);//可以随意多个
    cout<<n*m;
    return 0;
}

快写-输出一个

template<typename T>void write(T x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9)
    {
        write(x/10);
    }
    putchar(x%10+'0');
}

Example 3

#include<bits/stdc++.h>
using namespace std;
template<typename T>void write(T x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9)
    {
        write(x/10);
    }
    putchar(x%10+'0');
}
int n;
int main()
{
    cin>>n;
    write(n);
    return 0;
}

快写-输出多个

template<typename T>void write(T x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9)
    {
        write(x/10);
    }
    putchar(x%10+'0');
}
template<typename T, typename... Args> void write(T &first, Args& ... args) 
{
    write(first);
    write(args...);
}

Example 4

#include<bits/stdc++.h>
using namespace std;
template<typename T>void write(T x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9)
    {
        write(x/10);
    }
    putchar(x%10+'0');
}
template<typename T, typename... Args> void write(T &first, Args& ... args) 
{
    write(first);
    write(args...);
}
int n,m;
int main()
{
    cin>>n>>m;
    write(n,m);
    return 0;
}
发布了10 篇原创文章 · 获赞 2 · 访问量 2421

猜你喜欢

转载自blog.csdn.net/STL_CC/article/details/104813388