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

24 Feb, 2022
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:
Press  ctrl+alt+t     // This will open the terminalNow write sudo snap install sublime The installation will start just press “y”wherever it asks forand and hit enterCongrats sublime is now installed on your ubuntu.
Press  ctrl+alt+t     // This will open the terminal
Now write sudo snap install sublime 
The installation will start just press “y”wherever it asks forand and hit enter
Congrats sublime is now installed on your ubuntu.
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
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
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 
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:
Step 2: Now select View->Groups->Max columns: 2. 
This will group the last two created columns. See the image below:
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.
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
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.
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.
nikhil2001
gsumit178
C++
Competitive Programming
How To
Linux-Unix
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
Friend class and function in C++
Pair in C++ Standard Template Library (STL)
std::string class in C++
Competitive Programming - A Complete Guide
Practice for cracking any coding interview
Arrow operator -> in C/C++ with Examples
Modulo 10^9+7 (1000000007)
Prefix Sum Array - Implementation and Applications in Competitive Programming