C++ Fundamentals

Lesson 01 · 12 min

C++ Setup, Hello & I/O

How a C++ program runs, iostream, cin/cout, competitive programming fast I/O.

Hello, C++

Every C++ program needs an entry point: int main(). When main returns 0, the OS treats it as success.

Minimal program

#include <bits/stdc++.h>
using namespace std;

int main() {
    cout << "Hello CodeMentor\n";
    return 0;
}

#include <bits/stdc++.h> pulls almost all STL headers (common in contests). In production code, include only what you need (<iostream>, <vector>, …).

Reading & writing

GoalCode
Printcout << x << " " << y << "\n";
Read one valuecin >> n;
Read until failwhile (cin >> x)

Interview / LeetCode habit

On LeetCode you usually only write a class method — no main. Locally, CodeMentor wraps your method in a harness that feeds test input.

Fast I/O (contests)

ios::sync_with_stdio(false);
cin.tie(nullptr);

Do this once at the start of main in contest programs. Skip it inside LeetCode class methods.

Rules to remember

  • main must return int (usually 0).
  • Prefer '\n' over endl (endl flushes — slower).
  • cin >> skips whitespace; getline reads a full line.
  • On LeetCode: implement the method, not main.

Check yourself

1. What does return 0; from main usually mean?

2. On LeetCode Two Sum, what do you write?

Practice what you learned