Template for c++

Revision en5, by bshali, 2026-11-05 19:01:52

Hello guys! I have seen many c++ templates, and most of them are realy ugly. For the sake of speed they make code unreadable. However, using no template make you loose a bunch of time. Now I want to say what macros make template ugly:

1. Long templates

The reason for it is simple: you can't type it in a real contest. Writing long templates (over 30-50 lines) makes it too dificiult to type it in a contest. For me, using these templates is cheating. At that point you can add every possible algo to your template and call it a day. That's not the templates are for

2. Using macros instead of for loops

Currrently, many people use #define rep(i, a, b) or #define FOR and etc. Besides making code hard to debug, it doesn't decrease time siginificantly. I can understand using it occasionaly, but using rep(i, a, b) instead of clasicall for is diabolical for me.

3. Unnececary macros

Sometimes when i look others code i see for example #define MAX(a, b) a = max(a, b) or #define sum(a + b) (a + b) % MOD. If you really hate MODs (which is understanable) you may use Mint (an easy struct that can be written in contest). But these things happen very rarely.

There is much more to write, but the main things are those. Here is my template: ~~~~~ #include <bits/stdc++.h> using namespace std;

#define all(x) x.begin(), x.end()

int main() {
    cout << "Hello!";
}

~~~~~ There is some bad things here like using #define inf instead of adding it to const int. What i like about it: 1. It is short 2. It doesn't change the code drasticaly Btw there is a good tip: if you are using 1 based indexing and what to use x.begin(), x.end() you can use 1 + all(x). This works because 1 + all(x) = 1 + x.begin(), x.end(). Btw this is my first real blog :)

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en10 English bshali 2026-11-05 19:05:01 0 (published)
en9 English bshali 2026-11-05 19:04:42 4 Tiny change: 'cond\n #defin' -> 'cond\n #defin'
en8 English bshali 2026-11-05 19:04:25 59
en7 English bshali 2026-11-05 19:03:28 12
en6 English bshali 2026-11-05 19:03:02 522
en5 English bshali 2026-11-05 19:01:52 14
en4 English bshali 2026-11-05 19:01:21 622
en3 English bshali 2026-11-05 19:00:40 90
en2 English bshali 2026-11-05 18:57:53 20
en1 English bshali 2026-11-05 18:56:24 2333 Initial revision (saved to drafts)