I'm not gone to talk about popular templates as typedef long long ll; or typedef vector<int> vi;. I want to share some of mine templates, which I don't see vary often from other people, but think that this templates are very usefull.
It is often necessary to write something like x = max(x, something), and I would like to have a syntax like x max= something. I use this define for this.
#define cmax(u, v) u = max(u, v)
#define cmin(u, v) u = min(u, v)
Sometimes I forget that the solution in int does not work. And in order not to rewrite it, I will uncomment this define.
// #define int ll
Often the vector reading code looks the same, so I made a template function for this.
template<typename ValueType>
istream& operator>>(istream& in, vector<ValueType>& vect) { fore(vect) in >> val; return in; }
Also, for those who are interested, here is my complete template(someday I'll post it on github with all my DS and other stuffs, someday:D). Maybe you can learn something for yourself or give me some advice.
My entire template// #pragma GCC optimize ("O3")
#include <bits/stdc++.h>
typedef long long ll;
// #define int ll
using namespace ::std;
#define forn(i, n) for (int i = 0; i < n; ++i)
#define fore(collection) for (auto& val : collection)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define cmax(u, v) u = max(u, v)
#define cmin(u, v) u = min(u, v)
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<typename ValueType>
istream& operator>>(istream& in, vector<ValueType>& vect) { fore(vect) in >> val; return in; }
template<typename ValueType>
ostream& operator<<(ostream& out, const vector<ValueType>& vect) { fore(vect) out << val << " "; return out; }
void doWork()
{
// Your code goes here
}
signed main()
{
srand(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count());
ios_base::sync_with_stdio(false);
int t = 1;
// cin >> t;
while (t--) {
try {
doWork();
}
catch(const std::exception& e) {
std::cerr << e.what() << '\n';
return 1;
}
}
return 0;
}
And there is an ASKII art at the end of my template. But due to the fact that it is too big for the post, it's just screenshot.
Полный текст и комментарии »