A2Z Lesson

Log in
← Back to the A2Z sheet

Step 01 Beginner Problems — Language Basics

Learn C++

Easy

C++ is the default language of competitive programming and Indian campus placements: it runs fast, its STL library hands you sorted structures, heaps and hash maps for free, and most problem sets are tested against it. This lesson is an orientation — the skeleton of every C++ program, fast input/output, the core syntax you need for this sheet, and the road map for what to learn next.

The skeleton every program shares

Every C++ program starts from the same frame: include the library, use the std namespace, and put your logic in main(). The single header bits/stdc++.h pulls in the entire standard library, which is why competitive programmers never list individual includes. return 0 signals success.

#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; long long sum = 0; for (int i = 0; i < n; i++) { int x; cin >> x; sum += x; } cout << sum << '\n'; return 0; }

Input/output without the pain

cin and cout are convenient but slow by default because they stay synchronised with C streams. The two lines ios::sync_with_stdio(false) and cin.tie(nullptr) remove that overhead — paste them at the top of main() in every program. End output lines with '\n' rather than endl; endl also flushes the buffer, which is slow inside loops. When input is truly huge, scanf/printf or fast scanning exists, but with fast i/o switched on, cin handles contest-sized input fine.

The core syntax tour

Types: int (about ±2.1 billion), long long (about ±9.2 × 10^18 — use it whenever an answer might exceed int), double for decimals, char, bool, and string. Conditions and loops look like most C-family languages: if / else if / else, for (init; condition; update), while, and do-while.

Functions declare their return type and parameter types — int gcd(int a, int b). Arrays are fixed-size blocks: int a[100]; and the flexible workhorse is vector<int>, which grows dynamically. Strings support + concatenation and direct comparison. Two C++-specific habits matter from day one: integer division truncates (7 / 2 is 3), and indices are 0-based everywhere.

Your C++ road map for this sheet

In order: (1) this skeleton plus conditions and loops until pattern problems feel mechanical; (2) functions and recursion; (3) arrays and strings; (4) the STL lesson in this module — vector, pair, map, set, stack, queue, priority_queue and the sort/lower_bound algorithms. Everything else in C++ (classes, pointers in depth, templates) can wait until DSA is well under way. Judge yourself by problems solved, not chapters read.

Key points

  • One header (bits/stdc++.h) plus using namespace std plus main() is the universal C++ skeleton.
  • Always enable fast I/O: ios::sync_with_stdio(false); cin.tie(nullptr); and prefer '\n' over endl.
  • Default to long long for accumulated answers — int overflows near 2.1 billion.
  • Integer division truncates (7 / 2 == 3); all indexing is 0-based.
  • Road map: loops → functions/recursion → arrays/strings → STL. Judge progress by problems, not chapters.

Suggested questions

STL The companion lesson: vector, map, set, heap and the algorithms that make C++ shine.
Pattern 1 — Rectangular Star Pattern First reps with nested for-loops in your new skeleton.
Count all Digits of a Number ↗ while-loop and % practice — and a reminder that division truncates.
Basic Maths The concept tour behind the digit, divisor and GCD problems you are about to meet.

Source note: this lesson follows the structure of Striver's public A2Z DSA course (takeUforward). Each problem links to its original platform, and all problems and content belong to their respective owners — STUDY-BUDDY does not claim ownership of the sheet or its problems, and no permission beyond public linking is claimed.