I have been taking part in coding competitions for a few months now. I still have a lot to learn, but I thought I would share with you a beginner coding template that saves me a lot of time when starting a new problem.
The template is for C++ and based on modern C++ practices.
I have carefully looked at the code that top programmers use and they all follow roughly the same pattern. Also, I am not including all kinds of predefined functions, #define or typedef statements, etc. This is a bare-bones template to get started.
Codeforces problems come in two varieties regarding input: Ones with multiple test cases (usually problems A, B and C for more recent contests) and ones with a single test case (usually from problem D onward, but not always, as well as older contests).
Let's first look at problems with multiple test cases. Here is the template:
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
void solve() {
int n;
cin >> n;
// Your code here
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) solve();
}
#include <bits/stdc++.h>
: This line includes a header file that includes all standard C++ libraries. It's a commonly used shortcut, especially in competitive programming, to avoid including specific libraries individually.
using namespace std;
: This line imports all symbols from the std namespace into the current scope. While convenient, it can lead to namespace pollution and potential naming conflicts, especially in larger codebases. However, in competitive programming, where brevity and speed are crucial, it's often acceptable.
using vi = vector<int>;
: This line creates an alias vi
for the vector<int>
type. It can be useful for writing more concise code. More often than not, you will use it somewhere in your code.
void solve() { ... }
: This is a function definition for solving a single test case. Many problems start by requiring you to read in some value n
; thus I have added code to do that. If not required, it can be changed quickly to accommodate other types of input variables.
int main() { ... }
: This is the main function where program execution starts. Inside this function:
ios::sync_with_stdio(false);
and cin.tie(0);
are used to optimize I/O performance. If you are coding in C++ only (no C), it is common practice to include these two statements. It will significantly speed up your input/output performance.
int t;
and cin >> t;
: Reads the number of test cases from the input.
while (t--) solve();
repeatedly calls the solve function t
times, each time solving a single test case.
Now, suppose we need to solve only one test case. We can simplify the above code by removing the while
loop in the main()
function. The template becomes:
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
void solve() {
int n;
cin >> n;
// Your code here
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
}
Why not just do everything in main()
? One convenience of having a separate solve()
function outside of main()
is that you can very quickly create an additional solve2()
, solve3()
, etc. function. Suppose you submit the answer to a question and get a wrong answer (WA). Rather than deleting or changing solve()
, create solve2()
. You can now safely reference your code in solve()
. Just remember to call the right version of your function in main()
!
Here is an example:
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
void solve() {
// Your initial code here.
}
void solve2() {
// Your second attempt here.
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) solve2();
}
For C++, I am using Visual Studio Code as my editor. I have defined snippets to create the above two templates with a few keyboard shortcuts. This allows me to create the templates in a few seconds, rather than a minute or two (Yes, by the time you have typed out the template, the top programmers have likely completed the question!)
Happy coding!