- Competitive Programming is a mental sport that enables us to code a given problem under provided constraints. The purpose of this article is to guide every individual on how they can debug their code efficiently during a contest.
Prerequisite: Setting up Sublime Text for C++ Competitive Programming Environment
Time is something that is precious and it matters a lot during a coding contest. When writing a code, errors do come and programmers often tend to spend a lot of time debugging it. Â Often programmers deal with complex data structures during a contest and it is required to debug them in the given time constraint.Â
This article focuses on how to debug C++ source code efficiently in Sublime Text (IDE) during a contest and save time. First of all, it is required to set up the file structure of our Sublime Text. Below are the steps to set up the file structure of Sublime Text. Â
Step 1: Open the Sublime Text and follow the below steps:
1. Create three files:
- file.cpp: The file to write the code.
- inputf.txt: The file where we will be giving the input.
- outputf.txt: The file where the output will be displayed.
2. Now, perform the following steps:
- Select View > Layout > Columns: 3. This will create three columns in the workspace. Move the three files into three columns.
- Select View > Groups > Max Columns : 2 : input.txt and output.txt will get stacked in a single column.
Your Sublime Text would look similar to this:
Sublime Text
Step 2: Create a local function outside the main function. It is used to take input from the input.txt file and display output in the output.txt file. Below is the C++ code snippet for the same.
C++
// Declare this function outside // the main functionvoid local(){  // In case of online judges (like   // codechef, codeforces etc) these   // lines will be skipped. In other   // words these lines would be executed   // in Sublime Text only  #ifndef ONLINE_JUDGEÂ
  freopen("input.txt", "r", stdin);  freopen("output.txt", "w", stdout);Â
  // ONLINE_JUDGE  #endif } |
Â
Step 3: Call from the main function:Â
// Call from the main function local();
By combining the above steps our complete program would be:
C++
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;Â
// Local functionvoid local(){Â Â #ifndef ONLINE_JUDGEÂ Â freopen("input.txt", Â Â Â Â Â Â Â Â Â Â "r", stdin);Â Â freopen("output.txt", Â Â Â Â Â Â Â Â Â Â "w", stdout);Â Â Â Â Â // ONLINE_JUDGEÂ Â #endif }Â
// Driver codeint main(){Â Â local();Â Â return 0;} |
Â
Step 4: Now the IDE would look similar to this:
Sublime Text
Debugging using print function:
Create a print function in our program whenever we need to print a variable or any data structure like vector, set, map, etc. Below is the C++ program to implement the same approach:
C++
// C++ program to implement // the above approach#include <bits/stdc++.h>using namespace std;Â
// Driver codeint main(){  // Initializing a vector   vector<int> vect = {2, 4, 10,                       12, 17};Â
  // First operation  for (auto& x : vect)   {    if (x % 2 == 0)      x += 10;    else      x -= 10;  }Â
  // Second operation  for (auto& x : vect)    x += 2;Â
  // Third operation  for (auto& x : vect)    x += 20;} |
Â
Â
Suppose something went wrong in our logic due to which desired output is not obtained during a contest and hence to check the status of vector after the first operation, one can create a print function outside the main function which accepts a vector.
C++
// print function outside the // main function void print(vector<int>& vect){Â Â cout << "vect " << ' ';Â Â cout << '[' << ' ';Â
  // Print vector elements  for (auto x : vect)   {    cout << x << ' ';  }  cout << ']';} |
Â
Whenever there is a need to check the vector elements one can call the print() function by passing the vector as an argument to the print function.
// Calling print function from main print(vect);
Below is the complete C++ program to illustrate how to implement the above concept:
C++
// C++ program to implement// the above concept#include <bits/stdc++.h>using namespace std;Â
// Print function for debuggingvoid print(vector<int>& vect){Â Â cout << "vect " << ' ';Â Â cout << '[' << ' ';Â
  // Print vector elements  for (auto x : vect)   {    cout << x << ' ';  }  cout << ']';}Â
// Driver codeint main(){  // Initializing a vector  vector<int> vect = {2, 4, 10,                       12, 17};Â
  // First operation  for (auto& x : vect)   {    if (x % 2 == 0)      x += 10;    else      x -= 10;  }Â
  // Printing vect elements after   // applying first operation  // Checking the status of vect as   // a part of debugging  print(vect);Â
  // Second operation  for (auto& x : vect)    x += 2;Â
  // Third operation  for (auto& x : vect)    x += 20;Â
  int finalAnswer = 0;  for (auto x : vect)    finalAnswer += x;Â
  // Print the final answer  cout << "\nFinal Answer: " <<             finalAnswer;  return 0;} |
vect [ 12 14 20 22 7 ] Final Answer: 185
Disadvantages of this method:Â
- For the same data structure but having different data types there is a need to create multiple print functions. For example, there is a vector of integer type and a vector of string type, then in order to print elements, it is required to create two print functions outside the main function. One print function will accept a vector of integer type and another print function will accept a vector of string type.
- Contents of the vector would be printed along with the desired values in the same output.txt file which might be confusing for us.
- There is a need to comment statements that are used to call a print function from the main function, before eventually submitting the source code file to the online judge (Codeforces, Spoj, Codechef, etc).
Debugging using template:
In the above method, the data type of vector is hardcoded. The template can be used in C++. A template is a simple and yet very powerful tool in C++. The simple idea is to pass data type as a parameter so that there is no need to write the same code (print function) for different data types. Below is the C++ code snippet for the template:
C++
// One print function works for // all data types. This would work// even for user defined types if // operator '>' is overloadedtemplate <typename T>Â
void print(vector<T> vect){Â Â Â Â // body} |
Â
Below is the complete C++ program to illustrate the above concept:
C++
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;Â
// Using template so that this // function works for all data// typestemplate <typename T> void print(Â Â Â Â Â Â Â Â Â Â vector<T>& vect){Â Â cout << "vect " << ' ';Â Â cout << '[' << ' ';Â
  for (auto x : vect)   {    cout << x << ' ';  }  cout << ']';  cout << '\n';}Â
// Driver codeint main(){Â Â vector<int> vect1 = {2, 4, 10, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 12, 17};Â
  for (auto& x : vect1)   {    if (x % 2 == 0)      x += 10;    else      x -= 10;  }Â
  // Printing vect1 elements  print(vect1);Â
  // Initializing a vector of   // string type  vector<string> vect2 = {"Geeks",                           "for", "Geeks"};Â
  // Printing vect2 elements  print(vect2);Â
  // Modifying vect2  // push back string "is great"  vect2.push_back("is the great");Â
  // Printing vect2 after modification  print(vect2);Â
  int finalAnswer = 0;  for (auto x : vect1)    finalAnswer += x;  cout << "Final Answer: " <<            finalAnswer;  return 0;} |
vect [ 12 14 20 22 7 ] vect [ Geeks for Geeks ] vect [ Geeks for Geeks is the great ] Final Answer: 75
Â
A similar thing can be done with any data structure like a set, multiset, pairs, etc. Below is the implementation using the set:
C++
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;Â
// Using template so that this // function works for all data// typestemplate <typename T> void print(Â Â Â Â Â Â Â Â Â Â set<T>& set1){Â Â cout << "set " << ' ';Â Â cout << '[' << ' ';Â
  for (auto x : set1)   {    cout << x << ' ';  }  cout << ']';  cout << '\n';}Â
// Driver codeint main(){  // Declaring a set  set<int> set1;Â
  // Inserting elements in the set  for (int i = 0; i < 10; i++)    set1.insert(i);Â
  // Printing set1 elements  print(set1);Â
  // Declaring another set of   // string type  set<string> set2;Â
  // Inserting elements in the set  set2.insert("neveropen");Â
  // Printing set2 elements  print(set2);Â
  int finalAnswer = 0;     for (auto x : set1)    finalAnswer += x;  cout << "Final Answer: " <<            finalAnswer;  return 0;} |
set [ 0 1 2 3 4 5 6 7 8 9 ] set [ neveropen ] Final Answer: 45
Disadvantages of this method:Â
- The above method is not very efficient as each time before submitting the program there is a need to comment print statements inside the main function.
- Elements of the data structure would be printed along with other desired values in the same output.txt file which might be confusing for us.
Debugging using cerr:
The idea is to use the combination of cerr  (error stream) and file handling in the program. Create a separate file (error.txt) and use cerr stream instead of cout stream. Finally, with the help of file handling, print the status of data structure in the error.txt file.
Step 1: Firstly add the following snippets outside the main function:
- To print a variable we can create a print function with a template definition just above the function:
C++
//Template definitiontemplate <typename T>Â
//Function to print the variablevoid print(T x){  // Using error stream to print   // the variable  cerr << x;} |
Â
Â
- To print vector elements we can create a print function with a template definition just above the function:
C++
// Template definitiontemplate <typename T>Â
// Function to print the elements // of the vectorvoid print(vector<T>& a){Â Â cerr << '[' << ' ';Â
  for (auto x : a)   {    // Same to print a variable (Function    // Overloading)    print(x);Â
    cerr << ' ';  }  cerr << ']';} |
Â
Â
- To print set elements arranged in non-descending order we can create a print function with a template definition just above the function:
C++
//Template definitiontemplate <typename T>Â
// Function to print elements of the // set arranged in non-descending ordervoid print(set<T>& a){Â Â cerr << '[' << ' ';Â
  for (auto x : a)   {    // Same as printing a variable     // (Function Overloading)    print(x);Â
    cerr << ' ';  }  cerr << ']';} |
Â
Â
- To print set elements arranged in non-ascending order we can create a print function with a template definition just above the function:
C++
template <typename T>Â
// Function to print the set elements // arranged in non-ascending ordervoid print(set<T, greater<T> >& a){Â Â cerr << '[' << ' ';Â
  for (auto x : a)   {    // same as printing a variable     // (Function Overloading)    print(x);Â
    cerr << ' ';  }  cerr << ']';} |
Â
Â
- To print unordered set elements, we can create a print function with a template definition just above the function:
C++
// Template definitiontemplate <typename T>Â
// Function to print unordered // set elementsvoid print(unordered_set<T>& a){Â Â cerr << '[' << ' ';Â
  for (auto x : a)   {    // Same as printing a variable    // Using the concept of function     // overloading    print(x);         cerr << ' ';  }  cerr << ']';} |
Â
Â
- To print map elements, we can create a print function with a template definition just above the function:
C++
//Template definitiontemplate <typename T, typename V>Â
//Function to print map elements // arranged in non-descending ordervoid print(map<T, V>& a){Â Â cerr << "[ ";Â
  for (auto i : a)   {    // Same as variable using the     // concept of function overloading    print(i);Â
    cerr << " ";  }  cerr << "]";} |
Â
Â
- To print unordered map elements, we can create a print function with a template definition just above the function:
C++
//Template definitiontemplate <typename T, typename V>Â
//Function to print unordered map elementsvoid print(unordered_map<T, V>& a){  cerr << "[ ";  for (auto i : a)   {    // Same as variable using the     // concept of function overloading    print(i);         cerr << " ";  }  cerr << "]";} |
Â
Â
- To print multiset elements arranged in non-descending order, we can create a print function with a template definition just above the function:
C++
//Template definitiontemplate <typename T>Â
//Function to print multiset elements // arranged in non-descending ordervoid print(multiset<T>& a){Â Â cerr << '[' << ' ';Â
  for (auto x : a)   {    // Same as variable using the     // concept of function overloading    print(x);Â
    cerr << ' ';  }  cerr << ']';} |
Â
Â
- To print multiset elements, arranged in non-ascending order we can create a print function with a template definition just above the function:
C++
//Template definitiontemplate <typename T>Â
//Function to print elements of a // multiset arranged in non-ascending ordervoid print(multiset<T, greater<T> >& a){Â Â cerr << '[' << ' ';Â
  for (auto x : a)   {    // Same as variable using the     // concept of function overloading    print(x);Â
    cerr << ' ';  }  cerr << ']';} |
Â
Â
- To print unordered set elements, we can create a print function with a template definition just above the function:
C++
// Template definitiontemplate <typename T>Â
// Print function to print unordered // set elementsvoid print(unordered_set<T>& a){Â Â cerr << '[' << ' ';Â
  for (auto x : a)   {    // Same as variable using the     // concept of function overloading    print(x);Â
    cerr << ' ';  }  cerr << ']';} |
Â
Â
- To print vector of vectors elements, we can create a print function with a template definition just above the function:
C++
// Template definitiontemplate <typename T>Â
// Function to print vector of // vectors elementsvoid print(vector<vector<T> >& a){Â Â cerr << "[ ";Â
  for (auto i : a)   {    // Same as variable using the     // concept of function overloading    print(i);         cerr << " ";  }  cerr << "]";} |
Â
Â
- To print a pair of elements, we can create a print function with a template definition just above the function:
C++
// Template definitiontemplate <typename T, typename V>Â
// Function to print elements of a pairvoid print(pair<T, V> x){  // Same as printing the variable using  // the concept of function overloading  print(x.ff);     cerr << ':';Â
  // Same as variable using the concept   // of function overloading  print(x.ss);} |
- To print a pair of vectors elements, we can create a print function with a template definition just above the function:
C++
// Template definitiontemplate <typename T, typename V>Â
// Function to print vector of // pairs elementsvoid print(vector<pair<T, V> >& a){Â Â cerr << '[' << ' ';Â
  for (auto x : a)   {    // Same as printing a variable using     // the concept of function overloading    print(x.ff);    cerr << ":";Â
    // Same as printing a variable using     // the concept of function overloading    print(x.ss);    cerr << ' ';  }  cerr << ']';} |
Â
Step 2: Create one more new file (error.txt) and make sure it is in the same folder.
error.txt file: All the elements of the data structures that we have mentioned in the above snippets would be printed in this text file only without affecting the output.txt file.
Note that we want to write in this file using error stream (cerr). Again help from ifndef and endif preprocessor directives can be taken.
C++
// We want to skip writing on error.txt // file when online judge (Codechef, // Codeforces, etc) is defined #ifndef ONLINE_JUDGEÂ Â Â Â freopen("error.txt", "w", stderr);Â
// ONLINE_JUDGE#endif |
After adding the above lines in our local function, the complete function becomes:
C++
// Now local function would look like:void local(){Â Â #ifndef ONLINE_JUDGEÂ Â freopen("input.txt", "r", stdin);Â Â freopen("output.txt", "w", stdout);Â Â Â Â Â // ONLINE_JUDGEÂ Â #endif Â
  // It basically means that these   // statements (Between ifndef and   // endif) would be skipped / ignored   // if ONLINE_JUDGE is defined We don't   // need to comment "local();" statement   // in our function while submitting our   // source code file to online judges.  // It would be handled automaticallyÂ
  #ifndef ONLINE_JUDGE  freopen("error.txt", "w", stderr);     // ONLINE_JUDGE  #endif } |
Â
Â
Step 3: Also, we don’t want to comment debug(data_structure) statements while submitting the source code file to online judges. In simple words, there is a need to figure out a way so that debug functions will work for the sublime text (IDE) but they would be skipped/ignored for online judges.
Again, this can be achieved by using ifndef and endif preprocessor directives again in the source code.
C++
// If online judge is defined#ifndef ONLINE_JUDGE#define debug(x)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â cerr << #x << "Â ";Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â print(x);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â cerr << '\n';Â
// If Online Judge is not defined#else#define debug(x)#endif |
Â
Now the IDE would look similar to this:
Step 4: Whenever there is a need to check the status of any data structure, the following call can be made:
// Calling from the main function debug(dataStructure);
Step 5: Below is the implementation of the above method:
Example 1:
C++
/* It is recommended below snippets in    your template file of competitive programming */#include <bits/stdc++.h>using namespace std;Â
// Debugging FunctionsÂ
template<class T>void print(T x){  cerr << x;}template<class T, class V>         void print(pair<T , V> x){  print(x.ff);   cerr << ':';  print(x.ss);}template<class T>         void print(vector<T> &a){  cerr << '[' << ' ';  for(auto x : a)  {    print(x);    cerr << ' ';  }  cerr << ']';}template<class T>         void print(set<T> &a){  cerr << '[' << ' ';  for(auto x : a)  {    print(x);    cerr << ' ';  }  cerr << ']';}template<class T>         void print(set<T,                    greater<T>> &a){  cerr << '[' << ' ';  for(auto x : a)  {    print(x);    cerr << ' ';  }  cerr << ']';}template<class T>         void print(multiset<T> &a){  cerr << '[' << ' ';  for(auto x : a)  {    print(x);    cerr << ' ';  }  cerr << ']';}template<class T>         void print(multiset<T,                     greater<T>> &a){  cerr << '[' << ' ';  for(auto x : a)  {    print(x);    cerr << ' ';  }  cerr << ']';}template<class T>         void print(unordered_set<T> &a){  cerr << '[' << ' ';  for(auto x : a)  {    print(x);    cerr << ' ';  }  cerr << ']';}template<class T, class V>         void print(vector<pair<T, V>> &a){  cerr << '[' << ' ';  for(auto x : a)  {    print(x.ff);    cerr << ":";     print(x.ss);    cerr << ' ';  }  cerr << ']';}template <class T, class V>           void print(map <T, V> &a) {  cerr << "[ ";   for (auto i : a)   {    print(i);     cerr << " ";  }   cerr << "]";}template <class T, class V>           void print(unordered_map <T, V> &a) {  cerr << "[ ";   for (auto i : a)   {    print(i);     cerr << " ";  }   cerr << "]";}template <class T>           void print(vector<vector<T>> &a) {  cerr << "[ ";   for (auto i : a)   {    print(i);     cerr << " ";  }   cerr << "]";}Â
void local(){Â Â // ONLINE_JUDGEÂ Â #ifndef ONLINE_JUDGEÂ Â freopen("input.txt", "r", stdin);Â Â freopen("output.txt", "w", stdout);Â Â #endif Â
  // ONLINE_JUDGE  #ifndef ONLINE_JUDGE  freopen("error.txt", "w", stderr);  #endif Â
  #ifndef ONLINE_JUDGE  #define debug(x)   cerr << #x << " ";   print(x);   cerr << '\n';  #else  #define debug(x)  #endif}Â
// Driver codeint main(){Â Â local();Â
  // Number of elements in the vector  int n;Â
  // Taking input from the user   // through input.txt file  cin >> n;Â
  // Declaring a vector of integer   // type of size n  vector<int> vect1(n);Â
  // Initializing the vector     for(int i = 0 ; i < n ; i++)    cin >> vect1[i];Â
  // Modifying the vector   for (auto& x : vect1)   {    if (x % 2 == 0)      x += 10;    else      x -= 10;  }Â
  // Printing vect1 elements  // It will be printed in error.txt   // file using cerr stream  debug(vect1);Â
  // Initializing a vector of string type  vector<string> vect2 = {"Geeks", "for", "Geeks"};Â
  // Printing vect2 elements  // It will be printed in error.txt   // file using cerr stream  debug(vect2);Â
  // Modifying vect2  // push back string "is great"  vect2.push_back("is the great");Â
  // Printing vect2 after modification  // It will be printed in error.txt   // file using cerr stream  debug(vect2);Â
  // Calculating the answer  int finalAnswer = 0;  for (auto x : vect1)    finalAnswer += x;Â
  // Finally, printing answer in output.txt   // file using cout stream  cout << "Final Answer: " << finalAnswer;Â
  return 0;} |
file.cpp file:
input.txt file
output.txt fileÂ
error.txt file
Example 2:
C++
/* It is recommended below snippets in    your template file of competitive programming */Â
#include <bits/stdc++.h>using namespace std;Â
// Debugging Functionstemplate<class T>void print(T x){  cerr << x;}template<class T , class V>         void print(pair<T, V> x){  print(x.ff);   cerr << ':';  print(x.ss);}template<class T>         void print(vector<T> &a){  cerr << '[' << ' ';  for(auto x : a)  {    print(x);    cerr << ' ';  }  cerr << ']';}template<class T>         void print(set<T> &a){  cerr << '[' << ' ';  for(auto x : a)  {    print(x);    cerr << ' ';  }  cerr << ']';}template<class T>         void print(set<T,                    greater<T>> &a){  cerr << '[' << ' ';  for(auto x : a)  {    print(x);    cerr << ' ';  }  cerr << ']';}template<class T>         void print(multiset<T> &a){  cerr << '[' << ' ';  for(auto x : a)  {    print(x);    cerr << ' ';  }  cerr << ']';}template<class T>         void print(multiset<T,                     greater<T>> &a){  cerr << '[' << ' ';  for(auto x : a)  {    print(x);    cerr << ' ';  }  cerr << ']';}template<class T>         void print(unordered_set<T> &a){  cerr << '[' << ' ';  for(auto x : a)  {    print(x);    cerr << ' ';  }  cerr << ']';}template<class T, class V>         void print(vector<pair<T, V>> &a){  cerr << '[' << ' ';  for(auto x : a)  {    print(x.ff);    cerr << ":";    print(x.ss);    cerr << ' ';  }  cerr << ']';}template <class T, class V>           void print(map <T, V> &a) {  cerr << "[ ";   for (auto i : a)   {    print(i);     cerr << " ";  }   cerr << "]";}template <class T, class V>           void print(unordered_map <T, V> &a) {  cerr << "[ ";   for (auto i : a)   {    print(i);     cerr << " ";  }   cerr << "]";}template <class T>           void print(vector<vector<T>> &a) {  cerr << "[ ";   for (auto i : a)   {    print(i);     cerr << " ";  }   cerr << "]";}Â
void local(){Â Â // ONLINE_JUDGEÂ Â #ifndef ONLINE_JUDGEÂ Â freopen("input.txt", "r", stdin);Â Â freopen("output.txt", "w", stdout);Â Â #endif Â
  // ONLINE_JUDGE  #ifndef ONLINE_JUDGE  freopen("error.txt", "w", stderr);  #endifÂ
  #ifndef ONLINE_JUDGE  #define debug(x)   cerr << #x << " ";   print(x);   cerr << '\n';  #else  #define debug(x)  #endif}Â
// Driver codeint main(){Â Â local();Â
  // Number of elements in the set  int n;Â
  // Taking input from the user   // through input.txt file  cin >> n;Â
  // Declaring a set of integers  set<int> set1;Â
  for(int i = 0 ; i < n ; i++)  {    int number;Â
    // Taking input from the user     // through input.txt file    cin >> number;         //Inserting in the set     set1.insert(number);    }   Â
Â
  // Erasing from the set   if(!set1.empty())  {    // erasing the first element     // of the set    set1.erase(set1.begin());  }Â
  // Printing set1 elements  // It will be printed in error.txt   // file using cerr stream  debug(set1);Â
  // Declaring another set  set<string> set2;Â
  // Inserting in the set  set2.insert("neveropen");Â
  // Printing set2 elements  // It will be printed in error.txt file   // using cerr stream  debug(set2);Â
  // Inserting in set  // Insert the string "is great"  set2.insert("Geek");Â
  // Printing set2 elements after   // inserting into the set, It will  // be printed in error.txt file   // using cerr stream  debug(set2);Â
  // Calculating the answer  int finalAnswer = 0;  for (auto x : set1)    finalAnswer += x;Â
  // Finally, printing answer in output.txt   // file using cout stream  cout << "Final Answer: " << finalAnswer;Â
  return 0;} |
file.cpp file:
input.txt file
output.txt file
Â
error.txt file
Advantage of this method of debugging:
- Now there is no need to comment on each of the debug statements in the program before submitting the source code file to online judges.
- The data structure or STL elements would be printed in a separate file (error.txt) and desired output values would be printed in the output.txt file, making it more readable.
- In a nutshell, this can save a lot of time during a coding contest.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
