Learn C++
Learn programming basics in C++. Each lesson has a runnable example and notes on how other languages differ.
Basics
4 lessons- 01Printing to the screenYour first line of code: print text and the result of a calculation.
- 02Variables and valuesGive a value a name so you can reuse and change it.
- 03Reading inputRead text and numbers from the keyboard (standard input).
- 04Arithmetic and divisionAdd, subtract and multiply work the same everywhere — division doesn't.
Control flow
4 lessons- 05ConditionsDo different things depending on a condition: if · else if · else, plus switch.
- 06LoopsDo the same thing many times: for and while.
- 07More loopsTaking values one by one, going backwards, reading up to an end marker, and the run-at-least-once do-while.
- 08Booleans and the ternary operatorKeeping true/false in a variable, choosing one of two values, and asking "is it one of these?".
Data
5 lessons- 09StringsLength, uppercase, reverse, and picking one character.
- 10Lists and arraysMany values in a row: count, first and last, sum, transform all.
- 11Working with single charactersOne character as a value: its code, letter arithmetic, the i-th character, repeating.
- 12Building stringsBuild a string from pieces, join with a separator, print values on one line.
- 13Editing listsContains, remove, reverse, swap, fill with one value, copy.
Functions
2 lessonsGoing further
10 lessons- 16Dictionaries (maps)Find and update values by key: counting words, checking for a key, iterating in key order.
- 17SortingPut values in order: smallest first, largest first, or by a rule you choose.
- 182D lists (grids)Grids of rows and columns: reading them, row and column sums, and neighbours inside the bounds.
- 19Working with stringsSlicing, finding, replacing, trimming, comparing and padding.
- 20Integers and floating pointWhy 0.1 + 0.2 isn't 0.3, printing a fixed number of decimals, rounding rules and integer overflow.
- 21RecursionA function that calls itself: base cases, call-stack depth, loops and memoization.
- 22Common mistakes and error messagesHow to read error messages, and the mistakes every language shares: off-by-one, = vs ==, integer division, null, out-of-range indexes.
- 23Absolute value, powers, square rootsDropping the sign, multiplying many times, and finding the value whose square is a number.
- 24Counting, running totals, groupingIn one pass: count what matches, carry the value so far, and collect items by key.
- 25Lists of records and enumsCollect records in a list, and give names to a small fixed set of values.
Algorithms
8 lessons- 26Simulation and Brute ForceDo exactly what the story says, step by step, and try every candidate without skipping one.
- 27Stacks and QueuesA stack hands back the newest item first; a queue hands back the oldest first.
- 28Binary SearchThrow away half of a sorted range at a time to find a value, or the first position that meets a condition.
- 29Prefix sums and two pointersGet the sum of any range with one subtraction, and find a range by moving two positions forward only.
- 30GCD, primes and number basesFind the GCD with remainders, sift out primes with a sieve, and write a number in another base.
- 31The greedy method and priority queuesTake what looks best at each step, and use a queue that hands out the smallest value right away.
- 32Graphs and depth-first searchWrite down who is connected to whom, then visit everything you can reach.
- 33Breadth-first search and topological sortSpread out from the nearest places with a queue to get the fewest steps, and line tasks up so prerequisites come first.
Language-specific
5 lessons- 34PointersC++ · CCarry around where a value lives (its address) and change the caller's variable.
- 35Dynamic memoryC++ · CBorrow exactly as much memory as you need at run time, and always give it back.
- 36StructsC# · C++ · CBundle related values into one unit, and know whether that unit is copied or shared.
- 37Classes and objectsPython · JavaScript · TypeScript · Java · C# · C++Bundle data (fields) and behavior (methods) into one blueprint, then make many objects from it.
- 42C++ references and the STLC++& and const references, vector/map/set/unordered_map, algorithms that take lambdas, and the copy, invalidation and auto-insert traps.