Note 1
C++ Basics
Before learning algorithms and solving problems, we need to choose a language to use. There are many programming languages that we can choose from for competitive programming such as C++, Python, and Java. However, many competitors use C++ due to its speed and popularity [CS02-2]. I was debating whether to learn C++ or just stick with Python that I am already familiar with; however, I decided to learn C++ because it never hurts to learn a new language and it is also a recommended language [CS02-3]. With that in mind, let’s get started with basic syntax from C++!
1.1 Foundations
Let’s start with the Hello World example. However to do so, we need a text editor or an integrated development environment (IDE) and a compiler. There are many different options for text editor or IDE. There are online editors that lets you write and compile there. However, I highly recommend using local editors and compilers for maintainability. Among many local editors, some popular choices are Visual Studio Code, CLion, and Vim. Please don’t get mad for not including your favorite editor as it could get very philosophical. I personally use Neovim due to its low learning curve and ease in set up. Choosing an editor is up to you!
For compilers, two popular options are GNU Compiler Collection (GCC) and Clang. I personally use Clang, but this too is up to you! If you have an editor and a compiler of your choice, let’s get started with printing “Hello, World!” on our screen.
I know this looks a lot, but let’s first compile and see it before breaking down each line. To compile this, we can run
and execute it with the following commend.
Then, you should see the following output in your terminal.
Now let’s break down the program line by line.
First, #include
iostream> is for including the standard input
and output streams library. We need this to print our output on the
screen.
Next, int main() defines our main function for
execution. Next, std::cout << "Hello,World!\n"; is to
cout, or
character out, from the standard library specified with the namespace
std. We
also have \n to add a line break at the end to make a
pretty output. Finally, don’t forget your semicolon! We need to tell
our compiler that our statement has ended with the semicolon.
Meaning, unlike Python, indentation is only for writing a legible
code.
Lastly, return 0; is used to tell that the program
has successfully terminated. Nowadays, it isn’t strictly necessary as
reaching the end of the main function automatically returns \(0\). However, it’s still a good habit to
keep them at the end of the main function.
Below is an example with many outputs.
Compiling and executing returns the following.
Notice that we can add using namespace std; to avoid writing
std::
every time we need cout. We can do this for small projects and
speed in competitive programming. However, this is highly discouraged
in header files for large projects as it causes collisions. One more
thing to note is that both \n and endl returns a new line. However, they are
not the same thing! We will
return to this later when we discuss about optimization.
1.1.1 Variables
We know how to print a value. How about storing them? Let’s first start with definitions.
Definition 1.1.5
A region of a storage that a hardware can store a value is called an object. Variables are objects with a name. The data type is the type of value that can be stored in the object [CS02-1].
There are many data types and we need to specify the data type before declaring a variable. Below are some examples of basic data types.
| Types |
Stores... |
|
| int |
Integers |
|
| long long |
Larger 64-bit integers |
|
| float |
4-bit decimals |
|
| double |
8-bit decimals |
|
| char |
Single character |
|
| string |
Series of characters |
|
| bool |
True or false value |
|
With the data type in mind, we can declare a variable as the following.
Below is a quick example of declaring a variable and printing the value.
Notice that it is important to use quotation when declaring a string, and we can use simple operations when declaring a variable. This time, let’s take a look at another example where we can take in the value of a variable and declare variables of the same type.
When you compile and run it, we can get a simple calculator that can add two numbers with adequate size (32-bit).
If you don’t want the value of \(a\) to change, we can use const.
In this variation, \(a\) is fixed as \(5\) and cannot be changed.
One more thing to keep in mind about inputs is that some competitions and older USACO takes file as an input. In that case, we can do the same by using this template.
Now that we can take inputs and print outputs with various data types, let’s discuss about loops and conditions.