Pro setup of VS Code for Competitive Programming with C++ in Windows

Veeresh Maurya
5 min readMar 10, 2021

Visual studio code is one of the best IDE. If you getting start with Competitive Programming and you want to setup locally VS code IDE for Competitive Programming Then this article will help you.

Benefits of locally setup

Programming in your own IDE gives you a lot of benefits.

  • Intellisense: There is a Intellisense features in IDE which helps you to write code fast.
  • Debugging Tool : There is a powerful debugging tool in vs code which helps you to find errors in the program easily.
  • Snippets : Some lines of code are same for all programs so we can use snippets to save time.

Setup gcc/g++ compiler

For the gcc/g++ compiler, we will use the MinGW GNU compiler. You can download it from here and install it.

Follow these steps to setup Environment Variables in you system:-

  1. Search Advanced system settings form start menu.
  2. Click on Environment Variables .
  3. In system variable section find path and select it.
  4. Add a new variable C:\MinGW\bin .

Note- Please do not delete or edit any other Environment Variables .

Setup VSCode

If you do not have VS code download VS code, download VS code from here and install it in your windows machine.

Open VS code and install these few extensions

C/C++ for Visual Studio Code: This extension adds language support for C/C++ to Visual Studio Code, including features such as Intellisense and debugging.

Code Runner: This extension helps you to run code.

Setting up <bits/stdc++.h> and c++17

• C++ version 17 setup : STL is very useful in Competitive Programming era. But some SLT components and functions need above version cpp version 11 or 14, so we setup cpp version 17 in VS code.

  1. Go to VSCode setting (shortcut: Ctrl+, ) and search code-runner.
  2. Check run in terminal and save all file before run.
  3. And Go to Executor Map and Click Edit in setting.json.
  4. In cpp section add — std=c++17 between g++ and $fileName

Full line looks like this -

"cpp":"cd $dir && g++ --std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"

This line enables to run c++ program in version 17 and enable latest features of c++17.

  • <bits/stdc++.h> setup : It is basically a header file that includes all standard library. In programming contests, when you want to using this file is a good idea, when you want to reduce the time wasted in doing chores especially when your rank is time-sensitive.
  1. Go to directory : C:\MinGW\lib\gcc\mingw32\9.2.0\include\c++\mingw32\bits
  2. Right click while pressing Shift to open a Powershell/cmd window there. Run the command g++ -std=c++17 stdc++.h to compile the header file.
  3. To Check make sure that the stdc++.h.gch file was created in the directory.

Layout

Create three files program.cpp , input.txt & output.txt. Go toView > Editor Layout> Two columns, drag program.cpp file one side and input.txt & output.txt files to the other side. Drag input and output file as Row layout. Set the layout as per your convenience.

In the input.txt file, we will take input for the program and the output will be shown in the output.txt file.

Take an idea from my layout:-

Snippet

Write fast code is a important factor in competitive programming.So that you need your snippets or template. The snippet is totally on you. Make it according your convenience.

I have created a snippet with some useful macro and typedef. Have a look on my snippet:

CPP SNIPPET

To use snippet in VS code select File >Preferences>User Snippetsand then select cpp language and paste the following json code inside the curly braces.

"A CP snippet": {
"prefix": "cpsnip",
"body": [
"#include <bits/stdc++.h>",
"using namespace std;",
"#define gc getchar_unlocked",
"#define fo(i, n) for (i = 0; i < n; i++)",
"#define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)",
"#define ll long long",
"#define deb(x) cout << #x << \"=\" << x << endl",
"#define pb push_back",
"#define mp make_pair",
"#define F first",
"#define S second",
"#define all(x) x.begin(), x.end()",
"#define clr(x) memset(x, false, sizeof(x))",
"#define sortall(x) sort(all(x))",
"#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)",
"#define PI 3.1415926535897932384626",
"#define mod 1000000007",
"typedef pair<int, int> pii;",
"typedef pair<ll, ll> pl;",
"typedef vector<int> vi;",
"typedef vector<ll> vl;",
"typedef vector<pii> vpii;",
"typedef vector<pl> vpl;",
"typedef vector<vi> vvi;",
"typedef vector<vl> vvl;",
"int main()",
"{",
"#ifndef ONLINE_JUDGE",
" freopen(\"input.txt\", \"r\", stdin);",
" freopen(\"output.txt\", \"w\", stdout);",
"#endif",
" ios_base::sync_with_stdio(0);",
" cin.tie(0);",
" cout.tie(0);",
" int t;",
" cin >> t;",
" while (t--)",
" {",
" //write code here",
" }",
" return 0;",
"}"
],
"description": "A CP snippet"
}

To use this snippet just type cp and you see cpsnip in Intellisense section, hit enter.

Setup Editing C++

The C/C++ extension for VS Code has many features that help you write code, understand it, and navigate around in your source files.

To specify additional include directories to be searched, place your cursor over any #include directive that displays a green squiggle, then click the lightbulb action when it appears. This opens the file c_cpp_properties.json for editing; here you can specify additional include directories for each platform configuration individually by adding more directories to the 'browse.path' property.

Paste the following code in c_cpp_properties.json file.

c_cpp_properties.json

Setup Debugger

After the setting up above setting, Now the time to setup last thing that is Debugger. VSCode comes with a builtin nice GUI for debugging.To enable debugging you have to configure some files.

For debugging setup, Go to Run and click Start Debugging or click F5.

This will create a launch.json file under .vscode directory. Replace all code with given code.

launch.json

Now you need to add a build task.Go Terminal > Configure Default Build Task. A list of various predefined build tasks for C++ compilers will appear there as a dropdown.

Select C/C++: g++.exe build active file, This will create a task.json file under .vscode directory.

That’s all, Now You have a Pro setup for Competitive programming. These configurations will only for that particular directory.Make sure that if you are working in any other directory then repeat these debugging configurations or copy .vscode folder to that directory.

Conclusion

This is from my end, you are able you Customize if from yourself. Use Themes and other extentions to make your setup attractive and useful. If you have any problem regarding this setup feel free to ask in the comment section.

--

--