Skip to content

Instantly share code, notes, and snippets.

@leonsilicon
Last active June 21, 2020 18:30
Show Gist options
  • Select an option

  • Save leonsilicon/edb9a817a1e81bb77ca4b00f16edf581 to your computer and use it in GitHub Desktop.

Select an option

Save leonsilicon/edb9a817a1e81bb77ca4b00f16edf581 to your computer and use it in GitHub Desktop.
#ifdef ONLINE_JUDGE
#include <bits/stdc++.h>
using namespace std;
// Shortening STL container's member functions' names
#define R(f,t)template<typename...S>inline auto t(S&&...a)->decltype(this->f(forward<S>(a)...)){return this->f(forward<S>(a)...);}
#define alias(f,t) using t = f;
#define t_alias(f, t) template<typename ...T> using t = f<T...>;
#define iterator_aliases R(begin,b);R(end,e);R(rbegin,rb);R(rend,re);R(cbegin,cb);R(cend,ce);R(crbegin,crb);R(crend,cre);
#define unordered_aliases R(find,f);R(count,c);R(emplace,m);R(insert,i);R(erase,er);R(clear,cl);
#define ordered_aliases unordered_aliases;R(lower_bound,lb);R(upper_bound,ub);R(equal_range,eqr);
#define capacity_aliases R(empty,mt);R(size,s);
#define begin_alias(n)template<typename...T>struct n##_alias:public n<T...>{using n<T...>::n;
#define begin_tl_alias(n)struct n##_alias:public n{
#define end_alias(n)};
#define end_tl_alias(n)};
begin_alias(vector) iterator_aliases; capacity_aliases; R(push_back, pb); R(pop_back, ppb);
R(resize, rs); R(front, fr); R(back, bk); R(clear, cl); R(emplace, m); R(emplace_back, mb); R(insert, i); R(erase, er); end_alias(vector);
begin_alias(list) iterator_aliases; capacity_aliases; R(push_back, pb); R(pop_back, ppb);
R(push_front, pf); R(pop_front, ppf); R(resize, rs); R(front, f); R(back, bk);
R(clear, cl); R(emplace_front, mf); R(emplace_back, mb); end_alias(list);
begin_alias(set) iterator_aliases; ordered_aliases; capacity_aliases; end_alias(set);
begin_alias(multiset) iterator_aliases; ordered_aliases; capacity_aliases; end_alias(multiset);
begin_alias(unordered_set) R(begin, b); R(end, e); unordered_aliases; capacity_aliases; end_alias(unordered_set);
begin_alias(unordered_multiset) R(begin, b); R(end, e); unordered_aliases; capacity_aliases; end_alias(unordered_multiset);
begin_alias(map) iterator_aliases; ordered_aliases; capacity_aliases; end_alias(map);
begin_alias(multimap) iterator_aliases; ordered_aliases; capacity_aliases; end_alias(multimap);
begin_alias(unordered_map) R(begin, b); R(end, e); unordered_aliases; capacity_aliases; end_alias(unordered_map);
begin_alias(unordered_multimap) R(begin, b); R(end, e); unordered_aliases; capacity_aliases; end_alias(unordered_multimap);
begin_alias(stack) capacity_aliases; R(top, t); R(push, p); R(pop, pp); R(emplace, m); end_alias(stack);
begin_alias(queue) capacity_aliases; R(front, fr); R(back, b); R(push, p); R(emplace, m); R(pop, pp); end_alias(queue);
begin_alias(deque) capacity_aliases; R(push_back, pb); R(pop_back, ppb); R(emplace_back, mb); R(emplace_front, mf);
R(front, fr); R(back, bk); end_alias(deque);
begin_alias(priority_queue) capacity_aliases; R(top, t); R(push, p); R(emplace, m); R(pop, pp); end_alias(priority_queue);
begin_alias(basic_string)
basic_string_alias(basic_string<T...> &&s) : basic_string<T...>(s) {}
capacity_aliases; iterator_aliases; R(length, l); R(shrink_to_fit, stf); R(push_back, pb);
R(pop_back, ppb); R(insert, i); R(erase, er); R(replace, rep); R(substr, ss); R(find, f); R(rfind, rf);
R(find_first_of, ffo); R(find_last_of, flo); R(find_first_not_of, ffno); R(find_last_not_of, flno);
R(compare, comp); R(c_str, cs); R(back, bk); R(front, fr); end_alias(basic_string);
begin_alias(complex); R(imag, x); R(real, y); end_alias(complex);
template<typename T1, typename T2> struct pair_alias : pair<T1, T2> {
using pair<T1,T2>::pair;
inline T1 &x() {return this->first;}
inline T2 &y() {return this->second;}
inline T1 &f() {return this->first;}
inline T2 &s() {return this->second;}
inline T1 x() const {return this->first;}
inline T2 y() const {return this->second;}
inline T1 f() const {return this->first;}
inline T2 s() const {return this->second;}
};
#define string basic_string_alias<char>
#define pair pair_alias
#define complex complex_alias
#define list list_alias
#define vector vector_alias
#define set set_alias
#define multiset multiset_alias
#define unordered_set unordered_set_alias
#define unordered_multiset unordered_multiset_alias
#define map map_alias
#define multimap multimap_alias
#define unordered_map unordered_map_alias
#define unordered_multimap unordered_multimap_alias
#define stack stack_alias
#define queue queue_alias
#define deque deque_alias
#define priority_queue priority_queue_alias
#undef iterator_aliases
#undef ordered_aliases
#undef unordered_aliases
#undef capacity_aliases
#undef begin_alias
#undef begin_tl_alias
#undef end_alias
#undef end_tl_alias
#undef R
// shortening long type names
typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef long double ld;
typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd;
typedef tuple<int, int, int> t3i; typedef tuple<int, int, int, int> t4i; typedef tuple<ll, ll, ll> t3l; typedef tuple<ll, ll, ll, ll> t4l; typedef complex<int> point;
typedef complex<double> fpoint; typedef vector<int> vi; typedef vector<pii> vii; typedef vector<ll> vl;
typedef vector<pll> vll; typedef vector<t3i> v3i; typedef vector<t4i> v4i; typedef vector<vi> vvi; typedef vector<vii> vvii;
// pre-defined constants
const int iinf = 0x3f3f3f3f;
const ll linf = 0x3f3f3f3f3f3f3f3f;
#define nl '\n'
#define nls "\n"
#define cint const int
// ease-of-use functions
#define brk break
#define cont continue
inline void cppinput() {ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);}
inline int gc() {return getchar_unlocked();} inline int pc(int c) {return putchar_unlocked(c);}
template<typename T>constexpr inline T lg(T x){return sizeof(T)*8-(sizeof(T)>4?__builtin_clzll(x):__builtin_clz(x))-is_signed<T>::value;}
template<typename T> inline void scan(T& x){char _,_n;while((_n=gc())<45);if(_n-45)x=_n;else x=gc();
for(x-=48;47<(_=gc());x=(x<<3)+(x<<1)+_-48){}if(_n<46)x=-x;}
template <typename T, typename ...S>void scan(T& m, S&...s) {scan(m); scan(s...);}
template<typename T1, typename T2>ostream& operator<<(ostream &output, const pair<T1, T2> &p) {output << '(' << p.first << "," << p.second << ')';return output;}
template<typename T> constexpr const inline T& _max(const T& x, const T& y) {return x<y?y:x;}
template<typename T> constexpr const inline T& _min(const T& x, const T& y) {return x<y?x:y;}
template<typename T,typename ...S>constexpr const inline T& _max(const T& m, const S&...s){return _max(m,_max(s...));}
template<typename T,typename ...S>constexpr const inline T& _min(const T& m, const S&...s){return _min(m,_min(s...));}
#define max(...) _max(__VA_ARGS__)
#define min(...) _min(__VA_ARGS__)
#define gtt(type, ...) type __VA_ARGS__ ; scan(__VA_ARGS__);
#define gt(...) int __VA_ARGS__ ; scan(__VA_ARGS__);
template<typename T> inline void print(const T& n){T N=n;if(n<0){pc('-');N=-N;}T rev=N,count=0;if(N==0){pc('0');
return;}while((rev%10)==0){++count;rev/=10;}rev=0;while(N!=0){rev=(rev<<3)+(rev<<1)+N%10;N/=10;}while(rev!=0)
{pc(rev%10+'0');rev/=10;}while(count)pc('0'),--count;}
template<typename T, typename ...S> inline void print(const T& t, const S&...s){print(t); pc(' '); print(s...);}
#define itie(...) int __VA_ARGS__; tie(__VA_ARGS__)
#define ttie(type, ...) type __VA_ARGS__; tie(__VA_ARGS__)
#define trav(v, a) for (auto &v : a)
#define amax(a, b) (a = max(a, b))
#define amin(a, b) (a = min(a, b))
struct pair_hash {
template <class T1, class T2>
size_t operator() (const pair<T1, T2> &p) const {
return hash<T1>()(p.first) ^ hash<T2>()(p.second);
}
};
// support for variable argument looping macros
#define GET_MACRO(_1,_2,_3,NAME,...) NAME
#define lpu_e(e) for(uint _=0;_<e;++_)
#define lpu_ve(v,e) for(uint v=0;v<e;++v)
#define lpu_vbe(v,b,e) for(uint v=b;v<e;++v)
#define lpue_e(e) for(uint _=0;_<=e;++_)
#define lpue_ve(v,e) for(uint v=0;v<=e;++v)
#define lpue_vbe(v,b,e) for(uint v=b;v<=e;++v)
#define lp_e(e) for(int _=0;_<static_cast<int>(e);++_)
#define lp_ve(v,e) for(int v=0;v<static_cast<int>(e);++v)
#define lp_vbe(v,b,e) for(int v=b;v<static_cast<int>(e);++v)
#define lpe_e(e) for(int _=0;_<=static_cast<int>(e);++_)
#define lpe_ve(v,e) for(int v=0;v<=static_cast<int>(e);++v)
#define lpe_vbe(v,b,e) for(int v=b;v<=static_cast<int>(e);++v)
// Caution: the following loops always cast the integer to int, so make sure you don't risk overflows when
// using the following signed loops
#define lp(...) GET_MACRO(__VA_ARGS__, lp_vbe, lp_ve, lp_e)(__VA_ARGS__)
#define lpe(...) GET_MACRO(__VA_ARGS__, lpe_vbe, lpe_ve, lpe_e)(__VA_ARGS__)
#define lpu(...) GET_MACRO(__VA_ARGS__, lpu_vbe, lpu_ve, lpu_e)(__VA_ARGS__)
#define lpue(...) GET_MACRO(__VA_ARGS__, lpue_vbe, lpue_ve, lpue_e)(__VA_ARGS__)
#define raw_all(v,N) (v), ((v)+(N))
#define it_all(v) (v).begin(), (v).end()
#define all(...) GET_MACRO(__VA_ARGS__, _, raw_all, it_all)(__VA_ARGS__)
// shortening conditionals
#define elif else if
#define els else
#define whl while
#define ret return
// unary lambda
#define lamu(a) [&](auto &a)
// binary lambda
#define lamb(a, b) [&](auto &a, auto &b)
// shortening long STL names
#define rename_t(from, to) template<typename ...Ts> using to = from<Ts...>;
#define R(f,t)template<typename...S>inline static auto t(S&&...a)->decltype(f(forward<S>(a)...)){return f(forward<S>(a)...);}
// (I have the @ key mapped to SC:: on CLion)
struct SC {
// <algorithm>
R(all_of, alo);
R(any_of, ano);
R(none_of, no);
R(for_each, fe);
R(find, f);
R(find_first_of, ffo);
R(find_if, ff);
R(find_if_not, ffn);
R(count, c);
R(count_if, cf);
R(equal, eq);
R(is_permutation, ip);
R(search, sr);
R(copy, cp);
R(copy_if, cpf);
R(copy_backward, cpb);
R(move, mv);
R(move_backward, mvb);
R(swap, sw);
R(transform, tf);
R(replace, rp);
R(replace_if, rpf);
R(fill, fl);
R(fill, fln);
R(generate, gen);
R(generate_n, genn);
R(remove, rm);
R(remove_if, rmf);
R(unique, q);
R(rotate, rot);
R(random_shuffle, rs);
R(reverse, rv);
R(shuffle, sh);
R(sort, s);
R(stable_sort, ss);
R(memset, mms);
R(lower_bound, lb);
R(upper_bound, ub);
R(equal_range, eqr);
R(binary_search, bs);
R(min_element, mne);
R(max_element, mxe);
R(minmax_element, mme);
R(back_inserter, bi);
R(distance, d);
R(getline, gl);
R(print, p);
R(next_permutation, np);
R(prev_permutation, pp);
// <numeric>
R(accumulate, acc);
R(adjacent_difference, ad);
R(partial_sum, ps);
R(inner_product, ip);
// <functional>
R(bind1st, b1);
R(bind2nd, b2);
t_alias(greater, gr);
t_alias(greater_equal, gre);
t_alias(less, ls);
t_alias(less_equal, lse);
t_alias(minus, mi);
// STL Containers
alias(string, str);
alias(istringstream, iss);
t_alias(ostream_iterator, oi);
t_alias(vector, v);
t_alias(multiset, ms);
t_alias(unordered_set, us);
t_alias(unordered_multiset, ums);
t_alias(unordered_map, um);
t_alias(priority_queue, pq);
};
#undef R
#undef alias
#undef t_alias
#endif
#ifndef ONLINE_JUDGE
#include "cptemplate.h"
#endif
int main() {
}
@leonsilicon
Copy link
Author

leonsilicon commented Dec 26, 2019

There are some issues with this template, such as:

vii v; // vector of pair<int, int>
v.pb({1, 2}); // gives error: cannot find appropriate function for <brace-enclosed initializer list>

In this particular instance, one can use v.mb(1, 2) instead (emplace_back).
However, in other instances where a function similar to emplace doesn't work, you will have to manually specify the type:

set<pii> s;
s.lb(pii{1, 2}); // lower_bound

@leonsilicon
Copy link
Author

Also, if you're wondering how struct SC makes coding more efficient, I have @ mapped to SC:: on my machine, so instead of typing 11 characters for lower_bound, I can simply type @lb which gets expanded into SC::lb.

@leonsilicon
Copy link
Author

Done! I think I'm going to turn this template into a repository, which will make it easier to keep track of issues and would also allow me to explain how to use the template more :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment