Executing C++ code
Although this file was commmitted in 2025, it was written around the start of 2021.
C++ conversion course
[TOC]
I have been participating in competitive programming in Python. However, some challenges could only be solved a compiled language like C++.
The procedures detailed here introduces how to setup and use C++ and competitive programming.
Libraries
Usually we include individual libraries from the C++ Standard Template Library. To code faster, we import all the libraries at once, by adding #include <bits/stdc++.h> at the start.
However, we need to add the header file some directory on our local computer.
sudo cp template/stdc++.h /Library/Developer/CommandLineTools/usr/include/c++/v1/bits/
The header file is was obtained from here.
Compiling the code
g++ -std=c++17 -o tmp.out -Wall -Wno-unknown-pragmas x.cpp
Understanding the flags
-std=c++17compiles with the C++17 standard. Docs-o tmp.outspecifies the name of the compiled code. The default would have beena.outStack Overflow-Wallenables all the warnings. Docs-Wno-unknown-pragmasdisables warning for optimizer. Stack Overflow
Running the code
tmp.out < x0
Running for all test cases
A script has been written to compile x.cpp and run on all test cases x0, x1 etc.
./run_cpp.sh x
You can make this even more concise by adding alias cx="./run_cpp.sh" to ./zshrc, and the command is just
cx x
Auto-formatting
Indentation has helped me understand the written flow of the code, and whether the braces are closed in the correct places.
On MacOS, options + cmd + P is the shortcut to format the code on VSCode.
The default setting opens the curly bracket in a new line. To turn this off, go to File > Settings > C_Cpp.clang_format_fallbackStyle and replace Visual Studio with { BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0}.
Reference