Masoom Raj
Masoom Raj

Reputation: 71

Custom C++ code when creating new cpp file VS code

I use VS Code for compiling and running C++ code. But I want that whenever I create new cpp file, it already has this written

#include <bits/stdc++.h>
using namespace std;
#define ll long long

void solve (){
    // CODE HERE
}

int main () {

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

    solve();

    return 0;
}

Everytime I need to copy paste this code in new cpp file, is there any way so that it is already written when I create cpp files?

Upvotes: 0

Views: 259

Answers (1)

Yashovardhan Singh
Yashovardhan Singh

Reputation: 43

You can do that using custom snippet for C++ in VS code. In Visual Studio Code, snippets appear in IntelliSense and also support tab-completion. The snippets are made in JSON format. To add a custom snippet, you go to - File > Preferences and then select C++. Add the following code to the above location, or you can use this to create your snippet.

"Automatic Code": {
  "prefix": "!",
  "body": [
    "#include <bits/stdc++.h>",
    "using namespace std;",
    "#define ll long long",
    "",
    "void solve (){",
    "    // CODE HERE",
    "}",
    "",
    "int main () {",
    "",
    "    #ifndef ONLINE_JUDGE",
    "    freopen(\"input.txt\",\"r\",stdin);",
    "    freopen(\"output.txt\",\"w\",stdout);",
    "    #endif",
    "",
    "    solve();",
    "",
    "    return 0;",
    "}"
  ],
  "description": "Automatic Code"
}
  }

After this, you can type '!'(prefix attribute) to any C++ file and press Enter or Tab to make VS code write the code automatically.

Upvotes: 1

Related Questions