此文章用于总结在日常训练以及比赛时积累的可复用常见代码,以及可以加速代码书写的一些模板。

Features

  • 使用ACM常用的C++11标准
  • 定义了比赛常用long long,vector的简化名称
  • 引入简化版std::format,特化可迭代容器及元组,可以很方便的输出vector,map,tuple等STL自带数据结构
  • 常用的cmax和cmin函数
  • 类型友好型无限值
  • 多组数据支持
  • solution类,不用为手写资源重置操心了
#include <bits/stdc++.h>
using INT=int;
//#define int long long
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
using namespace std;
auto &_=std::ignore;
using ll=long long;
using vi=vector<int>;
using vll=vector<ll>;
using vvi=vector<vector<int>>;
using vvll=vector<vector<ll>>;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
constexpr struct{
	template<class T>
	constexpr operator T()const {return numeric_limits<T>::max();}
} INF;
constexpr struct{
	template<class T>
	constexpr operator T()const {return numeric_limits<T>::min();}
} MINF;
template<class T>
auto _format(ostream &os,const char *c,const T& cv)->decltype(os<<cv,c+1){
	os << cv;
	while (*c!='}') c++;
	return c+1;
}
template<size_t i,class T>
auto _format(ostream &os,const char *c,const T& cv)->decltype(
typename enable_if<i==tuple_size<T>::value>::type(1),c+1){return c;}
template<size_t i=0,class T>
auto _format(ostream &os,const char *c,const T& cv)->decltype(
typename enable_if<i!=tuple_size<T>::value>::type(1),c+1){
	while (*c!='{') os << *c++;
	c=_format(os,c,get<i>(cv));
	return _format<i+1>(os,c,cv);
}
template<class T>
auto _format(ostream &os,const char *c,const T& cv)->decltype(begin(cv),c+1){
	const char *ct=c+1;
	size_t ic=0;
	for (auto &i:cv){
		const char *cc=c+1;
		while (*cc!='{'){
			if (*cc=='i') os << ic,cc++;
			else os << *cc++;
		}
		cc=_format(os,cc,i);
		while (*cc!='}') os << *cc++;
		ct=cc;
		ic++;
	}
	return ct+1;
}
string format(const char *c){return string(c);}
template<class T,class ...Args>
string format(const char *c,const T &a,Args&& ...rest){
	ostringstream os;
	while (*c!='{'&&*c!='\0') os<< *c++;
	if (*c=='{') c=_format(os,c,a);
	return os.str()+format(c,forward<Args>(rest)...);
}
template<class ...Args>
ostream& print(Args&& ...rest){return cout<<format(forward<Args>(rest)...);}
#ifdef LOCAL
#define debug(...) cerr<<format(__VA_ARGS__)
#else
#define debug(...) cerr
#endif
template<class T>
void _read(istream& is,T &a){is>>a;}
void _read(istream& is,ll &a){
    a=0;bool f=false;char c;
    while (!isdigit(c=is.get())&&is.good()) f=c=='-';
    if (!is.good()) return ;
    do a=(a<<3)+(a<<1)+c-'0'; while (isdigit(c=is.get()));
    if (f) a=-a;
}

void read(){}
template<class T,class ...Args>
void read(T& a,Args& ...args){_read(cin,a);read(args...);}
template<class T>
void reada(vector<T>& v,size_t n){
	v.reserve(n);
	for (size_t i=0;i<n;i++){
		T a;cin>>a;
		v.emplace_back(move(a));
	}
}
template<class T1,class T2>
bool cmin(T1 &a,const T2 b){return a>b?a=b,1:0;}
template<class T1,class T2>
bool cmax(T1 &a,const T2 b){return a<b?a=b,1:0;}
template<class T1,class T2,class ...T3>
bool cmin(T1 &a,const T2 b,const T3 ...rest){return cmin(a,b)|cmin(a,rest...);}
template<class T1,class T2,class ...T3>
bool cmax(T1 &a,const T2 b,const T3 ...rest){return cmax(a,b)|cmax(a,rest...);}
bool MULTIDATA=true;
struct solution{

	void scan(){
        
	}
	
	void solve(){
		
	} 
};


INT main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int T=1;
	if (MULTIDATA) read(T);
	while (T--){
		auto a=unique_ptr<solution>(new solution());
		a->scan();
		a->solve();
		if (!cin.good()) break;
	}
	return 0;
}

使用例(仅重写solution类):

struct solution{
	map<int,int> m;
	void scan(){

	}
	
	void solve(){
		for (int i=0;i<12;i++) m[rand()]=rand();
		m[-2]=INF;
		m[-1]=MINF;
		print("{}\n{i=({},{})\n}",m.size(),m);
	} 
};

输出(可能的):

14
0=(-2,2147483647)
1=(-1,-2147483648)
2=(153,3902)
3=(491,9961)
4=(5436,4827)
5=(11942,2995)
6=(14604,32391)
7=(15724,19169)
8=(16827,23281)
9=(18467,41)
10=(24464,26962)
11=(26500,6334)
12=(28145,5705)
13=(29358,11478)