-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcpp.html
More file actions
1562 lines (1327 loc) · 89.5 KB
/
cpp.html
File metadata and controls
1562 lines (1327 loc) · 89.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>C++ - From Zero to Advanced - Better Dev</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header class="topbar">
<button class="sidebar-toggle" aria-label="Open navigation" aria-expanded="false">
<span class="hamburger-icon"></span>
</button>
<a href="index.html" class="logo">Better Dev</a>
</header>
<div class="sidebar-backdrop" aria-hidden="true"></div>
<aside class="sidebar" aria-label="Site navigation">
<div class="sidebar-header">
<span class="sidebar-title">Navigation</span>
<button class="sidebar-close" aria-label="Close navigation">×</button>
</div>
<div class="sidebar-search">
<input type="text" class="sidebar-search-input" placeholder="Search topics..." aria-label="Search topics">
<div class="sidebar-search-results"></div>
</div>
<nav class="sidebar-nav">
<div class="sidebar-group">
<a href="index.html">Home</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">Mathematics</div>
<a href="pre-algebra.html">Pre-Algebra</a>
<a href="algebra.html">Algebra</a>
<a href="sequences-series.html">Sequences & Series</a>
<a href="geometry.html">Geometry</a>
<a href="calculus.html">Calculus</a>
<a href="discrete-math.html">Discrete Math</a>
<a href="linear-algebra.html">Linear Algebra</a>
<a href="probability.html">Probability & Statistics</a>
<a href="binary-systems.html">Binary & Number Systems</a>
<a href="number-theory.html">Number Theory for CP</a>
<a href="computational-geometry.html">Computational Geometry</a>
<a href="game-theory.html">Game Theory</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">Data Structures & Algorithms</div>
<a href="dsa-foundations.html">DSA Foundations</a>
<a href="arrays.html">Arrays & Strings</a>
<a href="stacks-queues.html">Stacks & Queues</a>
<a href="hashmaps.html">Hash Maps & Sets</a>
<a href="linked-lists.html">Linked Lists</a>
<a href="trees.html">Trees & BST</a>
<a href="graphs.html">Graphs</a>
<a href="sorting.html">Sorting & Searching</a>
<a href="patterns.html">LeetCode Patterns</a>
<a href="dp.html">Dynamic Programming</a>
<a href="advanced.html">Advanced Topics</a>
<a href="string-algorithms.html">String Algorithms</a>
<a href="advanced-graphs.html">Advanced Graphs</a>
<a href="advanced-dp.html">Advanced DP</a>
<a href="advanced-ds.html">Advanced Data Structures</a>
<a href="leetcode-650.html">The 650 Problems</a>
<a href="competitive-programming.html">CP Roadmap</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">Languages & Systems</div>
<a href="cpp.html">C++</a>
<a href="golang.html">Go</a>
<a href="javascript.html">JavaScript Deep Dive</a>
<a href="typescript.html">TypeScript</a>
<a href="nodejs.html">Node.js Internals</a>
<a href="os.html">Operating Systems</a>
<a href="linux.html">Linux</a>
<a href="git.html">Git</a>
<a href="backend.html">Backend</a>
<a href="system-design.html">System Design</a>
<a href="networking.html">Networking</a>
<a href="cloud.html">Cloud & Infrastructure</a>
<a href="docker.html">Docker & Compose</a>
<a href="kubernetes.html">Kubernetes</a>
<a href="message-queues.html">Queues & Pub/Sub</a>
<a href="selfhosting.html">VPS & Self-Hosting</a>
<a href="databases.html">PostgreSQL & MySQL</a>
<a href="stripe.html">Stripe & Payments</a>
<a href="distributed-systems.html">Distributed Systems</a>
<a href="backend-engineering.html">Backend Engineering</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">JS/TS Ecosystem</div>
<a href="js-tooling.html">Tooling & Bundlers</a>
<a href="js-testing.html">Testing</a>
<a href="ts-projects.html">Building with TS</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">More</div>
<a href="seans-brain.html">Sean's Brain</a>
</div>
</nav>
</aside>
<div class="container">
<div class="page-header">
<div class="breadcrumb"><a href="index.html">Home</a> › C++</div>
<h1>C++ -- From Zero to Advanced</h1>
<p>Master C++ from the ground up: data types, control flow, pointers, memory management, RAII, the STL, and modern C++ features. Everything you need to write fast, safe, professional code.</p>
</div>
</div>
<div class="page-with-toc">
<aside class="sidebar-toc">
<div class="toc">
<h4>Table of Contents</h4>
<a href="#why-cpp">1. Why C++?</a>
<a href="#hello-world">2. Hello World & Basics</a>
<a href="#data-types">3. Data Types</a>
<a href="#variables-operators">4. Variables & Operators</a>
<a href="#control-flow">5. Control Flow</a>
<a href="#functions">6. Functions</a>
<a href="#pointers">7. Pointers</a>
<a href="#references">8. References</a>
<a href="#arrays">9. Arrays</a>
<a href="#strings">10. Strings</a>
<a href="#vectors">11. Vectors</a>
<a href="#memory">12. Memory Management</a>
<a href="#raii">13. RAII & Smart Pointers</a>
<a href="#classes">14. Structs & Classes</a>
<a href="#modern">15. Modern C++ Features</a>
<a href="#stl">16. STL Containers</a>
<a href="#patterns">17. Common Patterns</a>
<a href="#quiz">18. Practice Quiz</a>
</div>
</aside>
<div class="content">
<div class="container">
<div class="toc" style="display:none;" id="mobile-toc">
<h4>Table of Contents</h4>
<a href="#why-cpp">1. Why C++?</a>
<a href="#hello-world">2. Hello World & Basics</a>
<a href="#data-types">3. Data Types</a>
<a href="#variables-operators">4. Variables & Operators</a>
<a href="#control-flow">5. Control Flow</a>
<a href="#functions">6. Functions</a>
<a href="#pointers">7. Pointers</a>
<a href="#references">8. References</a>
<a href="#arrays">9. Arrays</a>
<a href="#strings">10. Strings</a>
<a href="#vectors">11. Vectors</a>
<a href="#memory">12. Memory Management</a>
<a href="#raii">13. RAII & Smart Pointers</a>
<a href="#classes">14. Structs & Classes</a>
<a href="#modern">15. Modern C++ Features</a>
<a href="#stl">16. STL Containers</a>
<a href="#patterns">17. Common Patterns</a>
<a href="#quiz">18. Practice Quiz</a>
</div>
<!-- ==================== SECTION 1 ==================== -->
<section id="why-cpp">
<h2>1. Why C++?</h2>
<p>C++ is one of the most powerful and widely used programming languages in the world. It gives you <strong>direct control over hardware and memory</strong> while still supporting high-level abstractions like classes, templates, and the STL.</p>
<h3>Where C++ Is Used</h3>
<ul>
<li><strong>Game engines:</strong> Unreal Engine, Unity's core, virtually every AAA game</li>
<li><strong>Operating systems:</strong> Windows, Linux kernel modules, macOS components</li>
<li><strong>Browsers:</strong> Chrome, Firefox, Safari are all written in C++</li>
<li><strong>Embedded systems:</strong> Robotics, IoT devices, automotive software</li>
<li><strong>Finance:</strong> High-frequency trading systems where microseconds matter</li>
<li><strong>Competitive programming:</strong> Most competitive programmers use C++ for speed</li>
<li><strong>Databases:</strong> MySQL, MongoDB, Redis</li>
</ul>
<h3>C++ vs Other Languages</h3>
<table>
<tr><th>Feature</th><th>C++</th><th>Python</th><th>Java</th></tr>
<tr><td>Speed</td><td>Extremely fast</td><td>Slow (interpreted)</td><td>Fast (JIT compiled)</td></tr>
<tr><td>Memory control</td><td>Full manual control</td><td>Garbage collected</td><td>Garbage collected</td></tr>
<tr><td>Learning curve</td><td>Steep</td><td>Gentle</td><td>Moderate</td></tr>
<tr><td>Use case</td><td>Systems, games, perf-critical</td><td>Scripting, ML, web</td><td>Enterprise, Android</td></tr>
</table>
<h3>How C++ Code Runs</h3>
<p>Unlike Python (interpreted line by line), C++ is <strong>compiled</strong>. Your code goes through several steps:</p>
<div class="formula-box">
Source code (.cpp) --> Preprocessor --> Compiler --> Object file (.o) --> Linker --> Executable
</div>
<p>You write <code>.cpp</code> files, the compiler (like <code>g++</code>) turns them into machine code, and the linker combines everything into an executable you can run.</p>
<div class="tip-box">
<div class="label">Key Mindset Shift</div>
<p>In Python, you think about <em>what</em> to do. In C++, you also think about <em>how memory works</em>. This is harder but gives you much more power and understanding of how computers actually work. Every concept in this page builds toward that understanding.</p>
</div>
</section>
<!-- ==================== SECTION 2 ==================== -->
<section id="hello-world">
<h2>2. Hello World and Basic Syntax</h2>
<pre><code><span class="lang-label">C++</span>
<span class="comment">// Every C++ program starts with #include and main()</span>
<span class="keyword">#include</span> <iostream> <span class="comment">// For input/output</span>
<span class="keyword">int</span> <span class="function">main</span>() {
std::cout << <span class="string">"Hello, World!"</span> << std::endl;
<span class="keyword">return</span> <span class="number">0</span>; <span class="comment">// 0 means "everything went fine"</span>
}
</code></pre>
<h3>Breaking It Down</h3>
<ul>
<li><code>#include <iostream></code> -- imports the input/output library (like <code>import</code> in Python)</li>
<li><code>int main()</code> -- the entry point. Every C++ program must have exactly one <code>main</code> function</li>
<li><code>std::cout</code> -- "character output" -- prints to the console. The <code><<</code> operator sends data to it</li>
<li><code>std::endl</code> -- prints a newline and flushes the buffer (you can also use <code>"\n"</code>)</li>
<li><code>return 0;</code> -- tells the OS the program succeeded</li>
<li>Every statement ends with a <strong>semicolon</strong> <code>;</code></li>
</ul>
<h3>Compiling and Running</h3>
<pre><code><span class="lang-label">Terminal</span>
<span class="comment"># Compile</span>
g++ -o hello hello.cpp
<span class="comment"># Run</span>
./hello
</code></pre>
<h3>Reading Input</h3>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">#include</span> <iostream>
<span class="keyword">int</span> <span class="function">main</span>() {
<span class="keyword">int</span> age;
std::cout << <span class="string">"Enter your age: "</span>;
std::cin >> age; <span class="comment">// Read from keyboard</span>
std::cout << <span class="string">"You are "</span> << age << <span class="string">" years old."</span> << std::endl;
<span class="keyword">return</span> <span class="number">0</span>;
}
</code></pre>
<div class="tip-box">
<div class="label">Using Namespace</div>
<p>Tired of typing <code>std::</code> everywhere? Add <code>using namespace std;</code> after your includes. This lets you write <code>cout</code> instead of <code>std::cout</code>. It's fine for learning and competitive programming, but avoided in production code because it can cause name conflicts.</p>
</div>
</section>
<!-- ==================== SECTION 3 ==================== -->
<section id="data-types">
<h2>3. Data Types</h2>
<p>In C++, every variable has a <strong>type</strong> that determines how much memory it uses and what values it can hold. Unlike Python, you must declare the type explicitly (or use <code>auto</code>).</p>
<h3>Primitive Types</h3>
<table>
<tr><th>Type</th><th>Size (typical)</th><th>Range</th><th>Example</th></tr>
<tr><td><code>bool</code></td><td>1 byte</td><td>true / false</td><td><code>bool alive = true;</code></td></tr>
<tr><td><code>char</code></td><td>1 byte</td><td>-128 to 127 (or a character)</td><td><code>char grade = 'A';</code></td></tr>
<tr><td><code>int</code></td><td>4 bytes</td><td>-2.1 billion to 2.1 billion</td><td><code>int count = 42;</code></td></tr>
<tr><td><code>long long</code></td><td>8 bytes</td><td>-9.2 quintillion to 9.2 quintillion</td><td><code>long long big = 1e18;</code></td></tr>
<tr><td><code>float</code></td><td>4 bytes</td><td>~7 decimal digits precision</td><td><code>float pi = 3.14f;</code></td></tr>
<tr><td><code>double</code></td><td>8 bytes</td><td>~15 decimal digits precision</td><td><code>double pi = 3.14159265;</code></td></tr>
<tr><td><code>void</code></td><td>0</td><td>No value (used for functions)</td><td><code>void doStuff();</code></td></tr>
</table>
<h3>Signed vs Unsigned</h3>
<p>By default, integers are <strong>signed</strong> (can be negative). Adding <code>unsigned</code> makes them non-negative but doubles the positive range:</p>
<div class="formula-box">
<strong>int:</strong> -2,147,483,648 to 2,147,483,647<br>
<strong>unsigned int:</strong> 0 to 4,294,967,295
</div>
<h3>Integer Overflow</h3>
<div class="warning-box">
<div class="label">Danger: Overflow</div>
<p>When a value exceeds its type's range, it <strong>wraps around</strong>. This causes bugs that are incredibly hard to find:</p>
<pre><code><span class="keyword">int</span> x = <span class="number">2147483647</span>; <span class="comment">// Max int value</span>
x = x + <span class="number">1</span>; <span class="comment">// OVERFLOW! x becomes -2147483648</span>
<span class="keyword">unsigned int</span> y = <span class="number">0</span>;
y = y - <span class="number">1</span>; <span class="comment">// UNDERFLOW! y becomes 4294967295</span></code></pre>
<p>Use <code>long long</code> when numbers might exceed ~2 billion. In competitive programming, always use <code>long long</code> to be safe.</p>
</div>
<h3>Type Casting</h3>
<pre><code><span class="lang-label">C++</span>
<span class="comment">// Implicit (automatic) -- compiler does it for you</span>
<span class="keyword">int</span> a = <span class="number">5</span>;
<span class="keyword">double</span> b = a; <span class="comment">// b is 5.0 (safe, no data loss)</span>
<span class="comment">// Implicit (dangerous) -- data loss!</span>
<span class="keyword">double</span> c = <span class="number">3.99</span>;
<span class="keyword">int</span> d = c; <span class="comment">// d is 3 (decimal part is LOST, not rounded)</span>
<span class="comment">// Explicit -- use static_cast to be clear about your intent</span>
<span class="keyword">double</span> e = <span class="number">9.7</span>;
<span class="keyword">int</span> f = <span class="keyword">static_cast</span><<span class="keyword">int</span>>(e); <span class="comment">// f is 9 (explicit, readable)</span>
</code></pre>
<h3>auto Keyword</h3>
<p>Let the compiler figure out the type for you:</p>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">auto</span> x = <span class="number">42</span>; <span class="comment">// int</span>
<span class="keyword">auto</span> y = <span class="number">3.14</span>; <span class="comment">// double</span>
<span class="keyword">auto</span> name = std::string(<span class="string">"Sean"</span>); <span class="comment">// std::string</span>
</code></pre>
<h3>const and constexpr</h3>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">const</span> <span class="keyword">int</span> MAX_SIZE = <span class="number">100</span>; <span class="comment">// Cannot be changed after initialization</span>
<span class="keyword">constexpr</span> <span class="keyword">int</span> SQUARE = <span class="number">5</span> * <span class="number">5</span>; <span class="comment">// Computed at COMPILE time (even faster)</span>
</code></pre>
<p><code>const</code> means "don't change this." <code>constexpr</code> means "compute this at compile time, not runtime." Use <code>constexpr</code> whenever the value is truly known at compile time.</p>
</section>
<!-- ==================== SECTION 4 ==================== -->
<section id="variables-operators">
<h2>4. Variables and Operators</h2>
<h3>Declaration and Initialization</h3>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">int</span> x; <span class="comment">// Declaration (DANGER: x has garbage value!)</span>
<span class="keyword">int</span> y = <span class="number">10</span>; <span class="comment">// C-style initialization</span>
<span class="keyword">int</span> z{<span class="number">10</span>}; <span class="comment">// Brace initialization (preferred in modern C++)</span>
<span class="keyword">int</span> w(<span class="number">10</span>); <span class="comment">// Constructor-style initialization</span>
</code></pre>
<div class="warning-box">
<div class="label">Always Initialize Variables</div>
<p>In C++, uninitialized variables contain <strong>garbage</strong> -- whatever happened to be in that memory location. This is a common source of bugs. Always give variables a starting value.</p>
</div>
<h3>Arithmetic Operators</h3>
<table>
<tr><th>Operator</th><th>Meaning</th><th>Example</th><th>Result</th></tr>
<tr><td><code>+</code></td><td>Addition</td><td><code>5 + 3</code></td><td>8</td></tr>
<tr><td><code>-</code></td><td>Subtraction</td><td><code>5 - 3</code></td><td>2</td></tr>
<tr><td><code>*</code></td><td>Multiplication</td><td><code>5 * 3</code></td><td>15</td></tr>
<tr><td><code>/</code></td><td>Division</td><td><code>7 / 2</code></td><td>3 (integer division!)</td></tr>
<tr><td><code>%</code></td><td>Modulus (remainder)</td><td><code>7 % 2</code></td><td>1</td></tr>
<tr><td><code>++</code></td><td>Increment</td><td><code>x++</code></td><td>x = x + 1</td></tr>
<tr><td><code>--</code></td><td>Decrement</td><td><code>x--</code></td><td>x = x - 1</td></tr>
</table>
<div class="warning-box">
<div class="label">Integer Division Trap</div>
<p><code>7 / 2</code> gives <strong>3</strong>, not 3.5! When both operands are integers, C++ does integer division (truncates). To get 3.5, make at least one operand a double: <code>7.0 / 2</code> or <code>static_cast<double>(7) / 2</code>.</p>
</div>
<h3>Comparison and Logical Operators</h3>
<table>
<tr><th>Operator</th><th>Meaning</th><th>Example</th></tr>
<tr><td><code>==</code></td><td>Equal to</td><td><code>x == 5</code></td></tr>
<tr><td><code>!=</code></td><td>Not equal to</td><td><code>x != 5</code></td></tr>
<tr><td><code><</code>, <code>></code></td><td>Less/greater than</td><td><code>x < 10</code></td></tr>
<tr><td><code><=</code>, <code>>=</code></td><td>Less/greater or equal</td><td><code>x >= 0</code></td></tr>
<tr><td><code>&&</code></td><td>Logical AND</td><td><code>x > 0 && x < 10</code></td></tr>
<tr><td><code>||</code></td><td>Logical OR</td><td><code>x == 0 || x == 1</code></td></tr>
<tr><td><code>!</code></td><td>Logical NOT</td><td><code>!done</code></td></tr>
</table>
<h3>Bitwise Operators</h3>
<table>
<tr><th>Operator</th><th>Meaning</th><th>Example</th></tr>
<tr><td><code>&</code></td><td>AND</td><td><code>5 & 3</code> = 1</td></tr>
<tr><td><code>|</code></td><td>OR</td><td><code>5 | 3</code> = 7</td></tr>
<tr><td><code>^</code></td><td>XOR</td><td><code>5 ^ 3</code> = 6</td></tr>
<tr><td><code>~</code></td><td>NOT (flip bits)</td><td><code>~5</code></td></tr>
<tr><td><code><<</code></td><td>Left shift</td><td><code>1 << 3</code> = 8</td></tr>
<tr><td><code>>></code></td><td>Right shift</td><td><code>8 >> 2</code> = 2</td></tr>
</table>
<div class="tip-box">
<div class="label">Bit Shift Trick</div>
<p><code>1 << n</code> is the fastest way to compute 2<sup>n</sup>. And <code>x >> 1</code> is the fastest way to divide by 2. You'll see these everywhere in competitive programming and systems code.</p>
</div>
</section>
<!-- ==================== SECTION 5 ==================== -->
<section id="control-flow">
<h2>5. Control Flow</h2>
<h3>if / else if / else</h3>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">int</span> score = <span class="number">85</span>;
<span class="keyword">if</span> (score >= <span class="number">90</span>) {
std::cout << <span class="string">"A"</span>;
} <span class="keyword">else if</span> (score >= <span class="number">80</span>) {
std::cout << <span class="string">"B"</span>;
} <span class="keyword">else if</span> (score >= <span class="number">70</span>) {
std::cout << <span class="string">"C"</span>;
} <span class="keyword">else</span> {
std::cout << <span class="string">"F"</span>;
}
</code></pre>
<h3>Ternary Operator</h3>
<p>A one-line if/else:</p>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">int</span> age = <span class="number">20</span>;
std::string status = (age >= <span class="number">18</span>) ? <span class="string">"adult"</span> : <span class="string">"minor"</span>;
</code></pre>
<h3>switch Statement</h3>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">char</span> grade = <span class="string">'B'</span>;
<span class="keyword">switch</span> (grade) {
<span class="keyword">case</span> <span class="string">'A'</span>: std::cout << <span class="string">"Excellent"</span>; <span class="keyword">break</span>;
<span class="keyword">case</span> <span class="string">'B'</span>: std::cout << <span class="string">"Good"</span>; <span class="keyword">break</span>;
<span class="keyword">case</span> <span class="string">'C'</span>: std::cout << <span class="string">"Average"</span>; <span class="keyword">break</span>;
<span class="keyword">default</span>: std::cout << <span class="string">"Invalid"</span>; <span class="keyword">break</span>;
}
</code></pre>
<div class="warning-box">
<div class="label">Don't Forget break!</div>
<p>Without <code>break</code>, execution "falls through" to the next case. This is a classic C++ bug. Every case should end with <code>break</code> unless you intentionally want fall-through.</p>
</div>
<h3>for Loop</h3>
<pre><code><span class="lang-label">C++</span>
<span class="comment">// Classic for loop</span>
<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < <span class="number">5</span>; i++) {
std::cout << i << <span class="string">" "</span>; <span class="comment">// 0 1 2 3 4</span>
}
<span class="comment">// Range-based for loop (modern C++)</span>
std::vector<<span class="keyword">int</span>> nums = {<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>};
<span class="keyword">for</span> (<span class="keyword">int</span> n : nums) {
std::cout << n << <span class="string">" "</span>; <span class="comment">// 10 20 30</span>
}
</code></pre>
<h3>while and do-while</h3>
<pre><code><span class="lang-label">C++</span>
<span class="comment">// while -- checks condition BEFORE each iteration</span>
<span class="keyword">int</span> i = <span class="number">0</span>;
<span class="keyword">while</span> (i < <span class="number">5</span>) {
std::cout << i << <span class="string">" "</span>;
i++;
}
<span class="comment">// do-while -- runs at least ONCE, checks condition AFTER</span>
<span class="keyword">int</span> input;
<span class="keyword">do</span> {
std::cout << <span class="string">"Enter a positive number: "</span>;
std::cin >> input;
} <span class="keyword">while</span> (input <= <span class="number">0</span>);
</code></pre>
<h3>break and continue</h3>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < <span class="number">10</span>; i++) {
<span class="keyword">if</span> (i == <span class="number">5</span>) <span class="keyword">break</span>; <span class="comment">// Exit the loop entirely</span>
<span class="keyword">if</span> (i % <span class="number">2</span> == <span class="number">0</span>) <span class="keyword">continue</span>; <span class="comment">// Skip to next iteration</span>
std::cout << i << <span class="string">" "</span>; <span class="comment">// Prints: 1 3</span>
}
</code></pre>
</section>
<!-- ==================== SECTION 6 ==================== -->
<section id="functions">
<h2>6. Functions</h2>
<h3>Basic Syntax</h3>
<pre><code><span class="lang-label">C++</span>
<span class="comment">// Declaration (prototype) -- tells compiler this function exists</span>
<span class="keyword">int</span> <span class="function">add</span>(<span class="keyword">int</span> a, <span class="keyword">int</span> b);
<span class="comment">// Definition -- the actual implementation</span>
<span class="keyword">int</span> <span class="function">add</span>(<span class="keyword">int</span> a, <span class="keyword">int</span> b) {
<span class="keyword">return</span> a + b;
}
<span class="comment">// Usage</span>
<span class="keyword">int</span> result = <span class="function">add</span>(<span class="number">3</span>, <span class="number">4</span>); <span class="comment">// result = 7</span>
</code></pre>
<h3>Pass by Value vs Pass by Reference</h3>
<p>This is one of the most important concepts in C++:</p>
<pre><code><span class="lang-label">C++</span>
<span class="comment">// Pass by VALUE -- function gets a COPY</span>
<span class="keyword">void</span> <span class="function">doubleIt</span>(<span class="keyword">int</span> x) {
x = x * <span class="number">2</span>; <span class="comment">// Only changes the local copy!</span>
}
<span class="keyword">int</span> num = <span class="number">5</span>;
<span class="function">doubleIt</span>(num);
<span class="comment">// num is STILL 5! The function only modified its own copy.</span>
<span class="comment">// Pass by REFERENCE -- function gets the ORIGINAL variable</span>
<span class="keyword">void</span> <span class="function">doubleIt</span>(<span class="keyword">int</span>& x) { <span class="comment">// Note the &</span>
x = x * <span class="number">2</span>; <span class="comment">// Changes the original!</span>
}
<span class="keyword">int</span> num = <span class="number">5</span>;
<span class="function">doubleIt</span>(num);
<span class="comment">// num is now 10!</span>
</code></pre>
<div class="tip-box">
<div class="label">When to Use Which</div>
<p><strong>Pass by value:</strong> When the function shouldn't modify the original (small types like int, double).</p>
<p><strong>Pass by reference:</strong> When you want to modify the original, or when copying is expensive (large objects).</p>
<p><strong>Pass by const reference:</strong> When you don't want to modify but also don't want to copy: <code>void print(const string& s)</code>. This is the go-to for strings, vectors, and any large object.</p>
</div>
<h3>Function Overloading</h3>
<p>C++ lets you have multiple functions with the same name but different parameter types:</p>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">int</span> <span class="function">add</span>(<span class="keyword">int</span> a, <span class="keyword">int</span> b) { <span class="keyword">return</span> a + b; }
<span class="keyword">double</span> <span class="function">add</span>(<span class="keyword">double</span> a, <span class="keyword">double</span> b) { <span class="keyword">return</span> a + b; }
<span class="function">add</span>(<span class="number">3</span>, <span class="number">4</span>); <span class="comment">// Calls int version, returns 7</span>
<span class="function">add</span>(<span class="number">3.0</span>, <span class="number">4.0</span>); <span class="comment">// Calls double version, returns 7.0</span>
</code></pre>
<h3>Default Arguments</h3>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">void</span> <span class="function">greet</span>(std::string name = <span class="string">"World"</span>) {
std::cout << <span class="string">"Hello, "</span> << name << <span class="string">"!"</span> << std::endl;
}
<span class="function">greet</span>(<span class="string">"Sean"</span>); <span class="comment">// Hello, Sean!</span>
<span class="function">greet</span>(); <span class="comment">// Hello, World!</span>
</code></pre>
</section>
<!-- ==================== SECTION 7 ==================== -->
<section id="pointers">
<h2>7. Pointers (The Big One)</h2>
<p>Pointers are C++'s most powerful and most feared feature. But they're not actually that hard once you understand the mental model.</p>
<h3>What Is a Pointer?</h3>
<p>Think of computer memory as a long street of houses. Each house has an <strong>address</strong> (a number) and a <strong>value</strong> (what's inside). A <strong>pointer</strong> is a variable that stores the address of another variable -- like writing down someone's house number on a piece of paper.</p>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">int</span> x = <span class="number">42</span>; <span class="comment">// A regular variable (a house with 42 inside)</span>
<span class="keyword">int</span>* ptr = &x; <span class="comment">// A pointer to x (stores x's address)</span>
std::cout << x; <span class="comment">// 42 (the value)</span>
std::cout << &x; <span class="comment">// 0x7fff5a2 (x's memory address)</span>
std::cout << ptr; <span class="comment">// 0x7fff5a2 (same address -- ptr stores it)</span>
std::cout << *ptr; <span class="comment">// 42 (dereference -- "go to the address and read the value")</span>
</code></pre>
<div class="formula-box">
<strong>&x</strong> = "address of x" (give me the house number)<br>
<strong>*ptr</strong> = "value at address stored in ptr" (go to that house and look inside)<br>
<strong>int*</strong> = "pointer to int" (a variable that holds an address of an int)
</div>
<h3>Memory Diagram</h3>
<div class="memory-diagram">
Stack Memory:
+----------+---------+------------+
| Variable | Value | Address |
+----------+---------+------------+
| x | 42 | 0x1000 |
| ptr | 0x1000 | 0x1008 | <-- ptr stores x's address
+----------+---------+------------+
*ptr means: "go to address 0x1000 and read what's there" --> 42
</div>
<h3>Modifying Through a Pointer</h3>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">int</span> x = <span class="number">42</span>;
<span class="keyword">int</span>* ptr = &x;
*ptr = <span class="number">100</span>; <span class="comment">// Change the value AT the address ptr points to</span>
std::cout << x; <span class="comment">// 100 -- x changed because ptr pointed at it!</span>
</code></pre>
<h3>nullptr</h3>
<p>A pointer that points to "nothing" -- always initialize pointers to <code>nullptr</code> if you don't have an address yet:</p>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">int</span>* ptr = <span class="keyword">nullptr</span>; <span class="comment">// Points to nothing (safe)</span>
<span class="comment">// *ptr would CRASH (segfault) -- never dereference nullptr!</span>
<span class="keyword">if</span> (ptr != <span class="keyword">nullptr</span>) {
std::cout << *ptr; <span class="comment">// Only dereference if not null</span>
}
</code></pre>
<h3>Pointer Arithmetic</h3>
<p>Adding to a pointer moves it forward by the size of the type it points to:</p>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">int</span> arr[] = {<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>};
<span class="keyword">int</span>* p = arr; <span class="comment">// Points to arr[0]</span>
std::cout << *p; <span class="comment">// 10</span>
std::cout << *(p+<span class="number">1</span>); <span class="comment">// 20 (next int, 4 bytes forward)</span>
std::cout << *(p+<span class="number">2</span>); <span class="comment">// 30</span>
</code></pre>
<h3>Pointer to Pointer (int**)</h3>
<p>A pointer can also point to another pointer. Think of it as: "I have a note with an address on it, and at that address there's another note with another address."</p>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">int</span> x = <span class="number">42</span>;
<span class="keyword">int</span>* p = &x; <span class="comment">// p points to x</span>
<span class="keyword">int</span>** pp = &p; <span class="comment">// pp points to p (pointer to pointer)</span>
std::cout << x; <span class="comment">// 42</span>
std::cout << *p; <span class="comment">// 42 (dereference once: go to x)</span>
std::cout << **pp; <span class="comment">// 42 (dereference twice: go to p, then go to x)</span>
</code></pre>
<div class="memory-diagram">
+----------+---------+------------+
| Variable | Value | Address |
+----------+---------+------------+
| x | 42 | 0x1000 |
| p | 0x1000 | 0x1008 | <-- p stores &x
| pp | 0x1008 | 0x1010 | <-- pp stores &p
+----------+---------+------------+
pp --> p --> x
**pp: go to pp(0x1008), read p(0x1000), go there, read 42
</div>
<h3>Pointer to Pointer to Pointer (int***)</h3>
<p>Yes, you can go deeper. Three levels of indirection:</p>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">int</span> x = <span class="number">42</span>;
<span class="keyword">int</span>* p = &x; <span class="comment">// Level 1: points to x</span>
<span class="keyword">int</span>** pp = &p; <span class="comment">// Level 2: points to p</span>
<span class="keyword">int</span>*** ppp = &pp; <span class="comment">// Level 3: points to pp</span>
std::cout << ***ppp; <span class="comment">// 42 (follow three arrows to reach x)</span>
</code></pre>
<div class="memory-diagram">
+----------+---------+------------+
| Variable | Value | Address |
+----------+---------+------------+
| x | 42 | 0x1000 |
| p | 0x1000 | 0x1008 |
| pp | 0x1008 | 0x1010 |
| ppp | 0x1010 | 0x1018 |
+----------+---------+------------+
ppp --> pp --> p --> x
***ppp = 42
</div>
<div class="tip-box">
<div class="label">When Would You Actually Use Multi-Level Pointers?</div>
<ul>
<li><strong>int** :</strong> Very common -- used for 2D arrays, arrays of strings in C, and functions that need to modify a pointer</li>
<li><strong>int*** :</strong> Rare in practice -- sometimes 3D arrays or complex data structures. If you find yourself here, consider using vectors or classes instead</li>
<li><strong>int**** :</strong> Almost never. If you need this, your design probably needs rethinking</li>
</ul>
</div>
<h3>Common Pointer Pitfalls</h3>
<div class="warning-box">
<div class="label">Dangers to Watch For</div>
<ul>
<li><strong>Dangling pointer:</strong> Points to memory that's been freed. Dereferencing it is undefined behavior (crash or garbage).</li>
<li><strong>Wild pointer:</strong> Never initialized. Contains a random address. Always initialize to <code>nullptr</code>.</li>
<li><strong>Memory leak:</strong> You <code>new</code> something but never <code>delete</code> it. The memory is gone until the program ends.</li>
<li><strong>Double free:</strong> Deleting the same memory twice. Causes crashes.</li>
</ul>
<p>Modern C++ largely solves these with smart pointers (Section 13).</p>
</div>
</section>
<!-- ==================== SECTION 8 ==================== -->
<section id="references">
<h2>8. References</h2>
<p>A <strong>reference</strong> is an alias (another name) for an existing variable. Unlike a pointer, it can't be null and can't be reassigned.</p>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">int</span> x = <span class="number">42</span>;
<span class="keyword">int</span>& ref = x; <span class="comment">// ref IS x (another name for the same memory)</span>
ref = <span class="number">100</span>;
std::cout << x; <span class="comment">// 100 -- changing ref changes x</span>
</code></pre>
<h3>References vs Pointers</h3>
<table>
<tr><th>Feature</th><th>Reference</th><th>Pointer</th></tr>
<tr><td>Syntax</td><td><code>int& ref = x;</code></td><td><code>int* ptr = &x;</code></td></tr>
<tr><td>Can be null?</td><td>No</td><td>Yes (nullptr)</td></tr>
<tr><td>Can be reassigned?</td><td>No (always refers to same variable)</td><td>Yes (can point to different things)</td></tr>
<tr><td>Must be initialized?</td><td>Yes</td><td>No (but should be)</td></tr>
<tr><td>Dereference needed?</td><td>No (use directly)</td><td>Yes (*ptr)</td></tr>
</table>
<div class="tip-box">
<div class="label">Rule of Thumb</div>
<p>Use <strong>references</strong> when you can, <strong>pointers</strong> when you must. References are safer and cleaner. Use pointers when you need nullptr, pointer arithmetic, or dynamic memory.</p>
</div>
</section>
<!-- ==================== SECTION 9 ==================== -->
<section id="arrays">
<h2>9. Arrays</h2>
<h3>C-Style Arrays</h3>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">int</span> arr[<span class="number">5</span>] = {<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>};
std::cout << arr[<span class="number">0</span>]; <span class="comment">// 10 (0-indexed!)</span>
std::cout << arr[<span class="number">4</span>]; <span class="comment">// 50</span>
<span class="comment">// arr[5] -- OUT OF BOUNDS! No error, just undefined behavior (scary)</span>
</code></pre>
<div class="warning-box">
<div class="label">No Bounds Checking</div>
<p>C-style arrays do NOT check if your index is valid. <code>arr[100]</code> won't give an error -- it'll just read random memory. This is a major source of security vulnerabilities. Prefer <code>std::vector</code> or <code>std::array</code> instead.</p>
</div>
<h3>std::array (Modern C++)</h3>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">#include</span> <array>
std::array<<span class="keyword">int</span>, <span class="number">5</span>> arr = {<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>};
std::cout << arr.<span class="function">size</span>(); <span class="comment">// 5 (knows its own size!)</span>
std::cout << arr.<span class="function">at</span>(<span class="number">2</span>); <span class="comment">// 30 (bounds-checked -- throws exception if out of range)</span>
</code></pre>
<h3>Array Decay to Pointer</h3>
<p>When you pass a C-style array to a function, it "decays" into a pointer to its first element and loses its size information:</p>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">void</span> <span class="function">printArray</span>(<span class="keyword">int</span>* arr, <span class="keyword">int</span> size) {
<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < size; i++)
std::cout << arr[i] << <span class="string">" "</span>;
}
<span class="comment">// You must pass size separately -- the array forgets it!</span>
</code></pre>
<p>This is one more reason to prefer <code>std::vector</code>.</p>
</section>
<!-- ==================== SECTION 10 ==================== -->
<section id="strings">
<h2>10. Strings</h2>
<h3>C-Strings (The Old Way)</h3>
<p>A C-string is just a <code>char</code> array ending with a null character <code>'\0'</code>:</p>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">char</span> name[] = <span class="string">"Hello"</span>; <span class="comment">// Actually stores: H e l l o \0</span>
<span class="comment">// Dangerous: no bounds checking, must manage \0 yourself</span>
</code></pre>
<div class="warning-box">
<div class="label">Avoid C-Strings</div>
<p>C-strings are error-prone (buffer overflows, missing null terminators). Use <code>std::string</code> for everything unless you're interfacing with C code.</p>
</div>
<h3>std::string (The Right Way)</h3>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">#include</span> <string>
std::string s1 = <span class="string">"Hello"</span>;
std::string s2 = <span class="string">"World"</span>;
<span class="comment">// Concatenation</span>
std::string s3 = s1 + <span class="string">" "</span> + s2; <span class="comment">// "Hello World"</span>
<span class="comment">// Length</span>
std::cout << s3.<span class="function">size</span>(); <span class="comment">// 11</span>
std::cout << s3.<span class="function">length</span>(); <span class="comment">// 11 (same thing)</span>
<span class="comment">// Access characters</span>
std::cout << s3[<span class="number">0</span>]; <span class="comment">// 'H'</span>
std::cout << s3.<span class="function">at</span>(<span class="number">4</span>); <span class="comment">// 'o' (bounds-checked)</span>
<span class="comment">// Comparison (works with == unlike C!)</span>
<span class="keyword">if</span> (s1 == <span class="string">"Hello"</span>) { <span class="comment">/* true */</span> }
<span class="comment">// Substring</span>
std::string sub = s3.<span class="function">substr</span>(<span class="number">0</span>, <span class="number">5</span>); <span class="comment">// "Hello"</span>
<span class="comment">// Find</span>
<span class="keyword">size_t</span> pos = s3.<span class="function">find</span>(<span class="string">"World"</span>); <span class="comment">// 6</span>
<span class="keyword">if</span> (pos != std::string::npos) { <span class="comment">/* found */</span> }
</code></pre>
<h3>Useful String Methods</h3>
<table>
<tr><th>Method</th><th>What It Does</th><th>Example</th></tr>
<tr><td><code>.size()</code> / <code>.length()</code></td><td>Number of characters</td><td><code>s.size()</code></td></tr>
<tr><td><code>.empty()</code></td><td>True if string is ""</td><td><code>s.empty()</code></td></tr>
<tr><td><code>.substr(pos, len)</code></td><td>Extract substring</td><td><code>s.substr(0, 5)</code></td></tr>
<tr><td><code>.find(str)</code></td><td>Find position of substring</td><td><code>s.find("hello")</code></td></tr>
<tr><td><code>.push_back(c)</code></td><td>Append a character</td><td><code>s.push_back('!')</code></td></tr>
<tr><td><code>.pop_back()</code></td><td>Remove last character</td><td><code>s.pop_back()</code></td></tr>
<tr><td><code>.append(str)</code></td><td>Append a string</td><td><code>s.append(" end")</code></td></tr>
<tr><td><code>.erase(pos, len)</code></td><td>Remove characters</td><td><code>s.erase(0, 3)</code></td></tr>
<tr><td><code>.c_str()</code></td><td>Get C-string version</td><td><code>s.c_str()</code></td></tr>
</table>
<h3>String Iteration</h3>
<pre><code><span class="lang-label">C++</span>
std::string word = <span class="string">"Hello"</span>;
<span class="comment">// By index</span>
<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < word.<span class="function">size</span>(); i++)
std::cout << word[i];
<span class="comment">// Range-based (modern, preferred)</span>
<span class="keyword">for</span> (<span class="keyword">char</span> c : word)
std::cout << c;
<span class="comment">// By reference (if modifying)</span>
<span class="keyword">for</span> (<span class="keyword">char</span>& c : word)
c = <span class="function">toupper</span>(c); <span class="comment">// Uppercase in-place</span>
</code></pre>
<h3>Reading Strings with Spaces</h3>
<pre><code><span class="lang-label">C++</span>
std::string name;
std::cin >> name; <span class="comment">// Reads only ONE word (stops at space)</span>
std::getline(std::cin, name); <span class="comment">// Reads entire line including spaces</span>
</code></pre>
</section>
<!-- ==================== SECTION 11 ==================== -->
<section id="vectors">
<h2>11. Vectors (std::vector)</h2>
<p>Vectors are <strong>dynamic arrays</strong> -- they grow and shrink automatically. They're the most commonly used container in C++ and should be your default choice over raw arrays.</p>
<h3>Why Vectors Over Arrays?</h3>
<ul>
<li>Dynamic size -- no need to know the size at compile time</li>
<li>Knows its own size (<code>.size()</code>)</li>
<li>Bounds checking with <code>.at()</code></li>
<li>Automatic memory management (no manual new/delete)</li>
</ul>
<h3>Creating and Using Vectors</h3>
<pre><code><span class="lang-label">C++</span>
<span class="keyword">#include</span> <vector>
<span class="comment">// Create</span>
std::vector<<span class="keyword">int</span>> v1; <span class="comment">// Empty vector</span>
std::vector<<span class="keyword">int</span>> v2 = {<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>}; <span class="comment">// Initialized</span>
std::vector<<span class="keyword">int</span>> v3(<span class="number">10</span>, <span class="number">0</span>); <span class="comment">// 10 elements, all 0</span>
<span class="comment">// Add elements</span>
v1.<span class="function">push_back</span>(<span class="number">10</span>); <span class="comment">// [10]</span>
v1.<span class="function">push_back</span>(<span class="number">20</span>); <span class="comment">// [10, 20]</span>
v1.<span class="function">push_back</span>(<span class="number">30</span>); <span class="comment">// [10, 20, 30]</span>
<span class="comment">// Remove last element</span>
v1.<span class="function">pop_back</span>(); <span class="comment">// [10, 20]</span>
<span class="comment">// Access</span>
std::cout << v2[<span class="number">0</span>]; <span class="comment">// 1 (no bounds check)</span>
std::cout << v2.<span class="function">at</span>(<span class="number">0</span>); <span class="comment">// 1 (bounds checked -- throws if invalid)</span>
std::cout << v2.<span class="function">front</span>(); <span class="comment">// 1 (first element)</span>
std::cout << v2.<span class="function">back</span>(); <span class="comment">// 5 (last element)</span>
<span class="comment">// Size</span>
std::cout << v2.<span class="function">size</span>(); <span class="comment">// 5</span>
std::cout << v2.<span class="function">empty</span>(); <span class="comment">// false</span>
</code></pre>
<h3>Iterating Over Vectors</h3>
<pre><code><span class="lang-label">C++</span>
std::vector<<span class="keyword">int</span>> nums = {<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>};
<span class="comment">// Method 1: Index-based</span>
<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < nums.<span class="function">size</span>(); i++)
std::cout << nums[i] << <span class="string">" "</span>;
<span class="comment">// Method 2: Range-based (preferred)</span>
<span class="keyword">for</span> (<span class="keyword">int</span> n : nums)
std::cout << n << <span class="string">" "</span>;
<span class="comment">// Method 3: Range-based by reference (can modify)</span>
<span class="keyword">for</span> (<span class="keyword">int</span>& n : nums)
n *= <span class="number">2</span>; <span class="comment">// Doubles every element in-place</span>
</code></pre>
<h3>2D Vectors</h3>
<pre><code><span class="lang-label">C++</span>
<span class="comment">// Create a 3x4 grid initialized to 0</span>
std::vector<std::vector<<span class="keyword">int</span>>> grid(<span class="number">3</span>, std::vector<<span class="keyword">int</span>>(<span class="number">4</span>, <span class="number">0</span>));
grid[<span class="number">1</span>][<span class="number">2</span>] = <span class="number">42</span>; <span class="comment">// Row 1, Column 2</span>
<span class="comment">// Iterate 2D</span>
<span class="keyword">for</span> (<span class="keyword">auto</span>& row : grid) {
<span class="keyword">for</span> (<span class="keyword">int</span> cell : row)
std::cout << cell << <span class="string">" "</span>;
std::cout << <span class="string">"\n"</span>;
}
</code></pre>
<h3>Common Vector Operations</h3>
<table>
<tr><th>Operation</th><th>Code</th><th>Time</th></tr>
<tr><td>Add to end</td><td><code>v.push_back(x)</code></td><td>O(1) amortized</td></tr>
<tr><td>Remove from end</td><td><code>v.pop_back()</code></td><td>O(1)</td></tr>
<tr><td>Access by index</td><td><code>v[i]</code> or <code>v.at(i)</code></td><td>O(1)</td></tr>
<tr><td>Insert at position</td><td><code>v.insert(v.begin()+i, x)</code></td><td>O(n)</td></tr>
<tr><td>Erase at position</td><td><code>v.erase(v.begin()+i)</code></td><td>O(n)</td></tr>
<tr><td>Size</td><td><code>v.size()</code></td><td>O(1)</td></tr>
<tr><td>Clear all</td><td><code>v.clear()</code></td><td>O(n)</td></tr>
<tr><td>Sort</td><td><code>sort(v.begin(), v.end())</code></td><td>O(n log n)</td></tr>
<tr><td>Reverse</td><td><code>reverse(v.begin(), v.end())</code></td><td>O(n)</td></tr>
</table>
<div class="tip-box">
<div class="label">reserve() for Performance</div>
<p>If you know roughly how many elements you'll add, call <code>v.reserve(n)</code> first. This pre-allocates memory so the vector doesn't have to resize (copy everything) multiple times as it grows.</p>
</div>
</section>
<!-- ==================== SECTION 12 ==================== -->
<section id="memory">
<h2>12. Memory Management</h2>
<h3>Stack vs Heap</h3>
<p>C++ uses two areas of memory:</p>
<div class="memory-diagram">
+---------------------------+
| STACK | Fast, automatic, limited size
| Local variables | Freed when function returns
| Function parameters | int x = 5; (lives here)
+---------------------------+
| |
| HEAP | Slower, manual, large
| Dynamically allocated | Freed when YOU say so
| new / delete | int* p = new int(5);
+---------------------------+
</div>
<table>
<tr><th>Feature</th><th>Stack</th><th>Heap</th></tr>
<tr><td>Speed</td><td>Very fast</td><td>Slower</td></tr>
<tr><td>Size</td><td>Small (~1-8 MB)</td><td>Large (GBs)</td></tr>
<tr><td>Lifetime</td><td>Automatic (scope-based)</td><td>Manual (you control)</td></tr>
<tr><td>Syntax</td><td><code>int x = 5;</code></td><td><code>int* p = new int(5);</code></td></tr>
</table>
<h3>new and delete</h3>
<pre><code><span class="lang-label">C++</span>
<span class="comment">// Allocate single value on heap</span>
<span class="keyword">int</span>* p = <span class="keyword">new</span> <span class="keyword">int</span>(<span class="number">42</span>);
std::cout << *p; <span class="comment">// 42</span>
<span class="keyword">delete</span> p; <span class="comment">// Free the memory</span>
p = <span class="keyword">nullptr</span>; <span class="comment">// Good practice: avoid dangling pointer</span>
<span class="comment">// Allocate array on heap</span>
<span class="keyword">int</span>* arr = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">5</span>]{<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>};
std::cout << arr[<span class="number">2</span>]; <span class="comment">// 3</span>
<span class="keyword">delete</span>[] arr; <span class="comment">// Free array (note the []!)</span>
</code></pre>
<div class="warning-box">
<div class="label">Memory Leak</div>
<p>If you <code>new</code> something but never <code>delete</code> it, that memory is leaked -- it can't be reused until the program exits. In a long-running server, this means your program slowly eats all available RAM and crashes.</p>
<pre><code><span class="keyword">void</span> <span class="function">leak</span>() {
<span class="keyword">int</span>* p = <span class="keyword">new</span> <span class="keyword">int</span>(<span class="number">42</span>);
<span class="comment">// Function returns without delete -- memory leaked!</span>
}</code></pre>
</div>
<div class="tip-box">
<div class="label">The Modern Rule</div>
<p>In modern C++, you should <strong>almost never use raw new/delete</strong>. Use smart pointers (next section) or containers like <code>std::vector</code> that manage memory for you. If you never write <code>new</code>, you can never leak.</p>
</div>
</section>
<!-- ==================== SECTION 13 ==================== -->
<section id="raii">
<h2>13. RAII and Smart Pointers</h2>
<p><strong>RAII</strong> stands for <strong>Resource Acquisition Is Initialization</strong>. It's the most important idiom in C++ and the reason C++ can be both low-level and safe.</p>
<h3>The Core Idea</h3>
<p>RAII means: <strong>tie the lifetime of a resource to the lifetime of an object.</strong></p>
<ul>
<li>When the object is <strong>created</strong> (constructor), it <strong>acquires</strong> the resource (memory, file handle, lock, etc.)</li>
<li>When the object is <strong>destroyed</strong> (destructor), it <strong>releases</strong> the resource</li>
<li>Since C++ guarantees destructors run when objects go out of scope, the resource is <strong>always</strong> cleaned up</li>
</ul>
<div class="example-box">
<div class="label">RAII in Action -- File Handling</div>
<pre><code><span class="comment">// BAD: Manual resource management (C-style)</span>
<span class="keyword">void</span> <span class="function">readFile</span>() {
FILE* f = fopen(<span class="string">"data.txt"</span>, <span class="string">"r"</span>);