刷题/笔试技巧之——C++常用模板

每次阅读大神的代码的时候,除了膜拜,还发现了大神都喜欢用自己的模板。

比如提前定义好变量、宏定义替换循环语句等。

这在笔试的时候能大大节省时间,所以参考了一些大神的代码,总结了一个还比较通用的模板贴出来与大家分享。

 


#include <bits/stdc++.h>  //万能头文件,仅限笔试/刷题用,正常学习不建议用(例如vs都不能使用)
using namespace std;
 
//shortcut
#define pb(x) 				push_back(x)
#define pf(x) 				push_front(x)
#define eb(x) 				emplace_back(x)
#define ef(x) 				emplace_front(x)
#define sz(v) 				((int)(v).size())
#define file_r(x) 			freopen(x,  "r",  stdin)
#define file_w(x) 			freopen(x,  "w",  stdout)
#define lowbit(x) 			((x) & (-x))
#define rep(i, n) 			for (int i = 0; i < (n); i++)
#define repd(i, n) 			for (int i = (n - 1); i >= 0; i--)
#define in1(n) 				scanf("%d", &n)
#define in2(n, m) 			scanf("%d %d", &n, &m)
#define in3(x, y, z) 		scanf("%d %d %d", &x, &y, &z)
#define sort_asc(c) 		sort(c.begin(), c.end())
#define iter(it, v) 		for (auto it = v.begin(); it != v.end(); ++ it)
 
//type defination
typedef long long Int;
 
//global associative variable
char charmap[256];
unordered_map<Int, Int> umii;
unordered_map<Int, string> umis;
unordered_map<string, Int> umsi;
unordered_set<Int> usi;
unordered_set<string> uss;
 
//global sequence variable
vector<Int> vi;
vector<string> vs;
list<Int> li;
list<string> ls;
 
//global ordinary variable
Int m, n;
size_t 	i, j, k;
 
//println
template <typename InputIterator>
void println(InputIterator first, InputIterator last)
{
	typedef typename InputIterator::value_type value_type;
	ostream_iterator<value_type> outile(cout, " ");
	copy(first, last, outile);
	cout << endl;
}
 
template <typename Container>
void println(Container &c)
{
	typedef typename Container::value_type value_type;
	ostream_iterator<value_type> outile(cout, " ");
	copy(c.begin(), c.end(), outile);
	cout << endl;
}
 
//input
template <typename Container>
void read_n(Container &c, int n)
{
	typedef typename Container::value_type value_type;
	istream_iterator<value_type> first(cin), last;
	for(; first != last && n; ++first, --n)
		c.push_back(*first);
}
 
template <typename Container>
void read(Container &c)
{
	typedef typename Container::value_type value_type;
	istream_iterator<value_type> first(cin), last;
	copy(first, last, back_inserter(c));
}
 
int main(void)
{
	cin >> n >> m;
	read_n(vi, m);
	println(vi);
	return 0;
}

 

;