杂七杂八的一些初始化相关注意事项

快读&快写

普通:

template<class T>
inline void read(T& x){
	int f = 0;
	char ch = getchar();
	while(!isdigit(ch))  f |= (ch == '-'), ch = getchar();
	while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48) , ch = getchar();
	x = f ? -x : x;
}

更快:

#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
template <typename T>
inline void read(T& r) {
	r=0;bool w=0; char ch=getchar();
	while(ch<'0'||ch>'9') w=ch=='-'?1:0,ch=getchar();
	while(ch>='0'&&ch<='9') r=r*10+(ch^48), ch=getchar();
	r=w?-r:r;
}
inline void out(int a){
    if(x<0){
        putchar('-');
        x=-x;
    }
    if(a>9)write(x/10);
    putchar(a%10+'0');
}

格式控制

cout << setiosflags(ios::left);
cout << setw(maxn[c] + 1) << str[r][c];

cout << std::fixed;
std::cout << std::setprecision(5) << f << '\n
cout << setw(3) << setfill('0') 

随机数

//rand()
#include<stdlib>
srand(time(0));
rand();//[0,2^15)
rand()<<15|rand(); //大于2^15
rand()%n; //[0,n)

//C++11 myrand
#include<random>
mt19937 myrand(time(0));
myrand(); //unsigned int
mt19937_64 myrand(time(0));
myrand(); //unsigned long long

memset

原理是把每个字节设成预定值。

memset(a, 0, sizeof(a));
memset(a, -1, sizeof(a));
memset(a, 0x3f, sizeof(a)); //1061109567
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值