Open In App

Setting Up Sublime Text For Competitive Coding in C++14 on Ubuntu

Last Updated : 24 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Started with coding learned the c++ language and basic ds&algo and want to more dive into it. Well, competitive coding is exactly what you need to give a boost to your coding skills. In this tutorial, we are starting with setting up cp environment so that you can start with your competitive programming journey.

Here we’re using the sublime text editor in Ubuntu. So if you have Ubuntu on your pc you are ready to go.

If you do not have sublime text editor on your Ubuntu you can easily install it. Just follow the below-given steps:

  1. Press  ctrl+alt+t     // This will open the terminal
  2. Now write sudo snap install sublime 
  3. The installation will start just press “y”wherever it asks forand and hit enter
  4. Congrats sublime is now installed on your ubuntu.

Setting up the build system 

For c++14 code to compile we need to set up a compiler for c++14 in the sublime text as it does not come by default.

Step 1: Open sublime text

Setting up sublime text for competitive coding in C++14(ubuntu)

Step 2: From the top menu, select Tools->Build system->New build system. On selecting this a new window will be opened as shown below

Setting up sublime text for competitive coding in C++14(ubuntu)

Step 3: Now all you have to do is to paste the below-given code in the tab opened. Make sure to erase the previous one 

{
  "cmd":["bash", "-c", "g++ -std=c++14 -Wall '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"],
  "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
  "working_dir": "${file_path}",
  "selector": "source.c, source.c++",
  "variants":
  [
    {
      "name": "Run",
      "cmd": ["bash", "-c", "g++ -std=c++14 '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
    }
  ]
}

The above code creates a C++14 build system for sublime since by default sublime has a default build system for c++11 so for making new features of c++14 to work in sublime we need to set up the build system for c++14.

Now, we have done setting up the build system for c++14 

Changing layout for I/O operations

In order for taking input and receiving output from a code, we need to manually set up our input and output files.

Step 1: From the top menu, select View->Layout->Columns :3 or press Shift+Alt+3. 

Three new columns will be created as shown below:

Setting up sublime text for competitive coding in C++14(ubuntu)

Step 2: Now select View->Groups->Max columns: 2. 

This will group the last two created columns. See the image below:

Setting up sublime text for competitive coding in C++14(ubuntu)

Step 3: Now you can view three files simultaneously in sublime text.

We will now select the first column(left) and save the file as main.cpp(This is the file where our code will be written). Similarly, select the second column(top-right), Press (Ctrl+N ), and then save the file as input.txt. At last, select the third column(top-right), Press (Ctrl+N ) and then save the file as output.txt.

Setting up sublime text for competitive coding in C++14(ubuntu)

Now, we have done changing the layout of sublime text for I/O operations.

This layout becomes extremely helpful when doing cp as in cp we all know to check our code for various types of input and check their outputs according to it so if all this happens in a single window the process becomes extremely fast. This layout is not a compulsion however is most preferred and used in cp

Linking I/O files with the main file

In order to link the main.cpp(program file) with input.txt(input file) and output.txt (output file) paste the below code in your main.cpp file in the main program.

Copy the entire code given below in the main.cpp file :

#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin); //file input.txt is opened in reading mode i.e "r"
freopen("output.txt","w",stdout);  //file output.txt is opened in writing mode i.e "w"
#endif

Input.txt and output.txt are files that we created for giving input to the program and receiving output respectively. The above given code line {  freopen(“input.txt”,”r”,stdin);  } justify that the program will take input from the mentioned file i.e input.txt, and you will the get desired output in output.txt as we mentioned it in the last code line {  freopen(“output.txt”,”w”,stdout);  }

Now, we have done setting up the cp environment in sublime text.

Working/Executing code

Write a sample program in your main.cpp file.

For reference, you can take the below-mentioned code:

C++




#include <bits/stdc++.h>
 
using namespace std;
 
int main()
 
{
 
#ifndef ONLINE_JUDGE
 
    freopen("input.txt", "r", stdin);
 
    freopen("output.txt", "w", stdout);
   
#endif
 
    string var = "geekforgeeks";
 
    cout << var;
 
    return 0;
}


After this save the main.cpp file and press (Ctrl+b) to run the file and your desired output will be displayed in the output.txt file.

Setting up sublime text for competitive coding in C++14(ubuntu)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads