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.

Code 1.1.1: CS0201.26050-01

1#include iostream 
2 
3int main() { 
4    std::cout  "Hello, World!\n"; 
5    return 0; 
6}

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

$ clang++ program.cpp -o program

and execute it with the following commend.

$ ./program

Then, you should see the following output in your terminal.

Output 1.1.2

Hello, World!

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.

Code 1.1.3: CS0201.26050-02

1#include iostream 
2using namespace std; 
3 
4int main() { 
5    cout  "I love banana. "  "Banana likes me"  endl; 
6    cout  "I ate five bananas today\n"; 
7 
8    return 0; 
9}

Compiling and executing returns the following.

Output 1.1.4

I love banana. Banana likes me 
I ate five bananas today

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.

1dataType variableName = value;

Below is a quick example of declaring a variable and printing the value.

Code 1.1.6: CS0201.26050-03

1#include iostream 
2 
3int main() { 
4    std::string name = "Enoch"; 
5    int numBananas = 12 + 34; 
6 
7    std::cout  "Hi! I am "  name  ", and I ate " 
8               numBananas  " bananas today.\n"; 
9 
10    return 0; 
11}

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.

Code 1.1.7: CS0201.26050-04

1#include iostream 
2 
3int main() { 
4    int a, b; 
5 
6    std::cout  "Type two numbers to add: "; 
7    std::cin  a  b; 
8    std::cout  "Your sum is: "  a + b  std::endl; 
9 
10    return 0; 
11}

When you compile and run it, we can get a simple calculator that can add two numbers with adequate size (32-bit).

Output 1.1.8

Type two numbers to add: 12345 67890 
Your sum is: 80235

If you don’t want the value of \(a\) to change, we can use const.

Code 1.1.9: CS0201.26050-05

1#include iostream 
2 
3int main() { 
4    const int a = 5; 
5    int b; 
6 
7    std::cout  "Type a number: "; 
8    std::cin  b; 
9    std::cout  b  " + 5 = "  a + b  std::endl; 
10 
11    return 0; 
12}

In this variation, \(a\) is fixed as \(5\) and cannot be changed.

Output 1.1.10

Type a number: 123456789 
123456789 + 5 = 123456794

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.

1#include cstdio 
2#include iostream 
3 
4int main() { 
5    freopen("inputFileName.in", "r", stdin); 
6    freopen("outputFileName.out", "w", stdout); 
7 
8    // Your code 
9}

Now that we can take inputs and print outputs with various data types, let’s discuss about loops and conditions.