Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to setup Competitive Programming in Visual Studio Code for C++

  • Difficulty Level : Medium
  • Last Updated : 19 Oct, 2021

GCC compiler installation

We need to install GCC compilers for Windows. Linux has already GCC installed.

Steps for installation

1.Download and Install the MinGW for GCC compiler using this link.
2.Open Control Panel in your system and then select: System (Control Panel)
3.Click on the Advanced system settings
4.Click on Environment Variables. In the section System Variables, find the PATH environment variable 
  and select it. Click Edit.
5.In the Edit System Variable window, specify the value of the PATH environment variable.
6.Type "C:\MinGW\bin"  
7.Click OK.
8.Close all remaining windows by clicking OK.

Images for additional reference

 



Important Extensions

1.Code Runner (By JunHan)---> To run the c++ code
2.C/C++ (By Microsoft)---> For intellisense and debugging
3.competitive-programming helper (By Divyanshu Agrawal) ---> Automatically Reads I/O for 
  codechef/codeforces

User Snippet/Startup Template for Competitive Programming

Below is the example for the template for competitive programming which programmers use. You can change this template according to your choice and preference:

C++




#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>  
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
   
using namespace std;
   
typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
ll MOD = 998244353;
double eps = 1e-12;
#define forn(i,e) for(ll i = 0; i < e; i++)
#define forsn(i,s,e) for(ll i = s; i < e; i++)
#define rforn(i,s) for(ll i = s; i >= 0; i--)
#define rforsn(i,s,e) for(ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
   
  
void solve(){
}
int main()
{
    fast_cin();
    ll t;
    cin >> t;
    for(int it=1;it<=t;it++) {
     cout << "Case #" << it+1 << ": ";
        solve();
    }
    return 0;
}

Steps to Create User Snippet/Startup Template

1.Open VS Code and go to: 
  File > Preferences > User Snippets
2.Click on New Snippets
3.Type cpp.json((can be anything).json)(the name of snippet)
4.Delete all the default code
5.Paste the json code given below
  (dont copy above C++ code!!, json files are needed for user snippets in VS Code)

Steps to use User Snippet/Startup template in a C++ file

1.Create a .cpp file(note: below snippet will only work once used in C++ file)
2.type the Snippet Trigger(look at "prefix" attribute in 3rd line of json file given below)
3.press tab or enter
  (the default prefix is set as gfg, you can alter it as per your need)

JSON Code for user snippet

Javascript




{
    "C++ Snippet": {
        "prefix": "gfg",
        "body": [
          "#pragma GCC optimize(\"Ofast\")",
          "#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma\")",
          "#pragma GCC optimize(\"unroll-loops\")",
          "#include <bits/stdc++.h>  ",
          "#include <complex>",
          "#include <queue>",
          "#include <set>",
          "#include <unordered_set>",
          "#include <list>",
          "#include <chrono>",
          "#include <random>",
          "#include <iostream>",
          "#include <algorithm>",
          "#include <cmath>",
          "#include <string>",
          "#include <vector>",
          "#include <map>",
          "#include <unordered_map>",
          "#include <stack>",
          "#include <iomanip>",
          "#include <fstream>",
          " ",
          "using namespace std;",
          " ",
          "typedef long long ll;",
          "typedef long double ld;",
          "typedef pair<int,int> p32;",
          "typedef pair<ll,ll> p64;",
          "typedef pair<double,double> pdd;",
          "typedef vector<ll> v64;",
          "typedef vector<int> v32;",
          "typedef vector<vector<int> > vv32;",
          "typedef vector<vector<ll> > vv64;",
          "typedef vector<vector<p64> > vvp64;",
          "typedef vector<p64> vp64;",
          "typedef vector<p32> vp32;",
          "ll MOD = 998244353;",
          "double eps = 1e-12;",
          "#define forn(i,e) for(ll i = 0; i < e; i++)",
          "#define forsn(i,s,e) for(ll i = s; i < e; i++)",
          "#define rforn(i,s) for(ll i = s; i >= 0; i--)",
          "#define rforsn(i,s,e) for(ll i = s; i >= e; i--)",
          "#define ln \"\\n\"",
          "#define dbg(x) cout<<#x<<\" = \"<<x<<ln",
          "#define mp make_pair",
          "#define pb push_back",
          "#define fi first",
          "#define se second",
          "#define INF 2e18",
          "#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)",
          "#define all(x) (x).begin(), (x).end()",
          "#define sz(x) ((ll)(x).size())",
          " ",
          "",
          "void solve(){",
          "}",
          "int main()",
          "{",
          "    fast_cin();",
          "    ll t;",
          "    cin >> t;",
          "    for(int it=1;it<=t;it++) {",
          "     cout << \"Case #\" << it+1 << \": \";",
          "        solve();",
          "    }",
          "    return 0;",
          "}"
        ],
        "description": "C++ Snippet"
      }
}

Images for additional reference

Basic Navigation

Select highlighted option to create a new snippet

“type cpp.json” the name of snippet, it should be a json file


Erase all this default code and paste the given json code

Note: In Prefix you have to use your personal choice word, here for example i am using gfg. This is very important because it is the key to call the template in your code.

Save this snippet by pressing CTRL+S. 

Open a new cpp file and write your prefix that you wrote in the prefix box.(Here i had used gfg).



Input.txt And Output.txt (Alternative To Competitive-Programming helper Extension)

All coding sites use a file comparison method to check answers. It means they store the output through your program in a text file and compare it with the actual answer file. Therefore, you should also do so. What you need to do is create a folder and inside it create 2 files input.txt, output.txt. 

First of all create the three required files. A cpp file containing your program.A text file input.txt and output.txt. Ensure that all three files are in the same directory. We need to divide the editor into three spaces

  1. Main code space 
  2. Input Space
  3. Output Space

Steps For Screen Setup

1.Once your cpp program is open, go to View>Editor Layout>Two Columns
2.In the empty(right) column right click and choose Split down
3.In the top window, right click and click on open file, open input.txt from the dropdown.
4.Repeat the same process in the bottom window. 
5.Paste the c++ code at the end of post just inside the main method 

Images for additional reference

Note: When you make any changes in input don’t forgot to save it by pressing ctrl+S in input.txt box.

Note: Don’t forgot to add this code after your main ()

C++




#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!