C++常用刷题模板

每次阅读大神的代码的时候,除了膜拜,还发现了大神都喜欢用自己的模板,比如提前定义好变量,宏定义替换循环语句等,这在笔试的时候能大大节省时间,所以参考了一些大神的代码,总结了一个还比较通用的模板贴出来与大家分享。

/*
*   Copyright (C) 2016 All rights reserved.
*   
*   filename: 		main.cpp
*   author: 		yuchenshu89757
*   e-mail: 		yuchenshu89757@163.com
*   create time: 	2016-08-07 12:30:31
*   last modified: 	2016-08-07 12:30:31
*/

#include <bits/stdc++.h>
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;
}

水平有限,不完善的地方敬请大神批评指正,谢谢!

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值