Benefits of Terminal in Competitive Programming

Note: This is just a simple article telling the benefits of using terminal. I am not saying against anything.

I have seen many competitive programmers asking the choice of IDEs and why multiple competitive programmers prefer the terminal. I am creating this blog so that there is one place where we can share different tricks in which terminal can help us. Please share your tricks as well.

Compiling in the terminal you can easily add and remove flags to the process (This can be done using other methods as well but the terminal is just easy)

Let’s say I have two debugging part

#ifdef BRIDGES
printf("HERE ARE THE BRIDGES\n");
for(auto &i: bridges)printf("%d %d\n",i.first,i.second);
#endif

#ifdef DELETED_EDGES
printf("HERE ARE THE DELETED EDGES\n");
for(auto &i: deleted_edges)printf("%d %d\n",i.first,i.second);
#endif

You don’t have to re-comment them again and again as Online Judge won’t be passing these flags
and you can easily debug your code only seeing what you want to see

g++ -DDELETED_EDGES -DBRIDGES a.cpp -o a.out
g++ -DDELETED_EDGES a.cpp -o a.out
g++ -DBRIDGES a.cpp -o a.out

Infact you can create a runner file to compile multiple executables and

run.sh :

g++ -DDELETED_EDGES -DBRIDGES a.cpp -o edge_bridge
g++ -DDELETED_EDGES a.cpp -o edge
g++ -DBRIDGES a.cpp -o bridge

And then just run ./run.sh

On top of that, you can find cases where your solution is wrong using a test case generator and bash script.

Let’s say you have a test case generator (link to my simple generator) and you write a brute force solution brute.cpp

Just create a simple runner shell file

run.sh

g++ a.cpp -o normal
g++ brute.cpp -o brute
for i in {0..10}
do
#Generate test case
python3 test.py
(./normal < input.txt)>out1
(./brute < input.txt)>out2
diff out1 out2
done

You can, in fact, add cases to stop the loop when out1 and out2 differ to stop at a test case where the answer is wrong. I don’t know yet how to do that. Please do tell me if you know how to do it.

Another benefit of using the terminal can be you can choose your version of C++ standard on the go.

g++ -std=gnu++11 a.cpp -o a.out
g++ -std=gnu++14 a.cpp -o a.out
g++ -std=gnu++17 a.cpp -o a.out

Or adding extra warning flags.

g++ -Wall a.cpp -o a.out
g++ -Wextra a.cpp -o a.out

Also, you do not need to copy-paste the sample test case again and again.

  1. Copy TC from Judge
  2. cat > inp (name other test case inp2, inp3 and so on)
  3. Paste test case
  4. Press Ctrl + D

To run it

  1. g++ a.cpp -o a.out
  2. ./a.out < inp
17 Likes