-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2 Intro to the C++ Language.html
2466 lines (2234 loc) · 165 KB
/
2 Intro to the C++ Language.html
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>
<head>
<title>2 Intro to the C++ Language.md</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<style>
/* https://github.com/microsoft/vscode/blob/master/extensions/markdown-language-features/media/markdown.css */
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
body {
font-family: var(--vscode-markdown-font-family, -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif);
font-size: var(--vscode-markdown-font-size, 14px);
padding: 0 26px;
line-height: var(--vscode-markdown-line-height, 22px);
word-wrap: break-word;
}
#code-csp-warning {
position: fixed;
top: 0;
right: 0;
color: white;
margin: 16px;
text-align: center;
font-size: 12px;
font-family: sans-serif;
background-color:#444444;
cursor: pointer;
padding: 6px;
box-shadow: 1px 1px 1px rgba(0,0,0,.25);
}
#code-csp-warning:hover {
text-decoration: none;
background-color:#007acc;
box-shadow: 2px 2px 2px rgba(0,0,0,.25);
}
body.scrollBeyondLastLine {
margin-bottom: calc(100vh - 22px);
}
body.showEditorSelection .code-line {
position: relative;
}
body.showEditorSelection .code-active-line:before,
body.showEditorSelection .code-line:hover:before {
content: "";
display: block;
position: absolute;
top: 0;
left: -12px;
height: 100%;
}
body.showEditorSelection li.code-active-line:before,
body.showEditorSelection li.code-line:hover:before {
left: -30px;
}
.vscode-light.showEditorSelection .code-active-line:before {
border-left: 3px solid rgba(0, 0, 0, 0.15);
}
.vscode-light.showEditorSelection .code-line:hover:before {
border-left: 3px solid rgba(0, 0, 0, 0.40);
}
.vscode-light.showEditorSelection .code-line .code-line:hover:before {
border-left: none;
}
.vscode-dark.showEditorSelection .code-active-line:before {
border-left: 3px solid rgba(255, 255, 255, 0.4);
}
.vscode-dark.showEditorSelection .code-line:hover:before {
border-left: 3px solid rgba(255, 255, 255, 0.60);
}
.vscode-dark.showEditorSelection .code-line .code-line:hover:before {
border-left: none;
}
.vscode-high-contrast.showEditorSelection .code-active-line:before {
border-left: 3px solid rgba(255, 160, 0, 0.7);
}
.vscode-high-contrast.showEditorSelection .code-line:hover:before {
border-left: 3px solid rgba(255, 160, 0, 1);
}
.vscode-high-contrast.showEditorSelection .code-line .code-line:hover:before {
border-left: none;
}
img {
max-width: 100%;
max-height: 100%;
}
a {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
a:focus,
input:focus,
select:focus,
textarea:focus {
outline: 1px solid -webkit-focus-ring-color;
outline-offset: -1px;
}
hr {
border: 0;
height: 2px;
border-bottom: 2px solid;
}
h1 {
padding-bottom: 0.3em;
line-height: 1.2;
border-bottom-width: 1px;
border-bottom-style: solid;
}
h1, h2, h3 {
font-weight: normal;
}
table {
border-collapse: collapse;
}
table > thead > tr > th {
text-align: left;
border-bottom: 1px solid;
}
table > thead > tr > th,
table > thead > tr > td,
table > tbody > tr > th,
table > tbody > tr > td {
padding: 5px 10px;
}
table > tbody > tr + tr > td {
border-top: 1px solid;
}
blockquote {
margin: 0 7px 0 5px;
padding: 0 16px 0 10px;
border-left-width: 5px;
border-left-style: solid;
}
code {
font-family: Menlo, Monaco, Consolas, "Droid Sans Mono", "Courier New", monospace, "Droid Sans Fallback";
font-size: 1em;
line-height: 1.357em;
}
body.wordWrap pre {
white-space: pre-wrap;
}
pre:not(.hljs),
pre.hljs code > div {
padding: 16px;
border-radius: 3px;
overflow: auto;
}
pre code {
color: var(--vscode-editor-foreground);
tab-size: 4;
}
/** Theming */
.vscode-light pre {
background-color: rgba(220, 220, 220, 0.4);
}
.vscode-dark pre {
background-color: rgba(10, 10, 10, 0.4);
}
.vscode-high-contrast pre {
background-color: rgb(0, 0, 0);
}
.vscode-high-contrast h1 {
border-color: rgb(0, 0, 0);
}
.vscode-light table > thead > tr > th {
border-color: rgba(0, 0, 0, 0.69);
}
.vscode-dark table > thead > tr > th {
border-color: rgba(255, 255, 255, 0.69);
}
.vscode-light h1,
.vscode-light hr,
.vscode-light table > tbody > tr + tr > td {
border-color: rgba(0, 0, 0, 0.18);
}
.vscode-dark h1,
.vscode-dark hr,
.vscode-dark table > tbody > tr + tr > td {
border-color: rgba(255, 255, 255, 0.18);
}
</style>
<style>
/* Tomorrow Theme */
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
/* Original theme - https://github.com/chriskempson/tomorrow-theme */
/* Tomorrow Comment */
.hljs-comment,
.hljs-quote {
color: #8e908c;
}
/* Tomorrow Red */
.hljs-variable,
.hljs-template-variable,
.hljs-tag,
.hljs-name,
.hljs-selector-id,
.hljs-selector-class,
.hljs-regexp,
.hljs-deletion {
color: #c82829;
}
/* Tomorrow Orange */
.hljs-number,
.hljs-built_in,
.hljs-builtin-name,
.hljs-literal,
.hljs-type,
.hljs-params,
.hljs-meta,
.hljs-link {
color: #f5871f;
}
/* Tomorrow Yellow */
.hljs-attribute {
color: #eab700;
}
/* Tomorrow Green */
.hljs-string,
.hljs-symbol,
.hljs-bullet,
.hljs-addition {
color: #718c00;
}
/* Tomorrow Blue */
.hljs-title,
.hljs-section {
color: #4271ae;
}
/* Tomorrow Purple */
.hljs-keyword,
.hljs-selector-tag {
color: #8959a8;
}
.hljs {
display: block;
overflow-x: auto;
color: #4d4d4c;
padding: 0.5em;
}
.hljs-emphasis {
font-style: italic;
}
.hljs-strong {
font-weight: bold;
}
</style>
<style>
/*
* Markdown PDF CSS
*/
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif, "Meiryo";
padding: 0 12px;
}
pre {
background-color: #f8f8f8;
border: 1px solid #cccccc;
border-radius: 3px;
overflow-x: auto;
white-space: pre-wrap;
overflow-wrap: break-word;
}
pre:not(.hljs) {
padding: 23px;
line-height: 19px;
}
blockquote {
background: rgba(127, 127, 127, 0.1);
border-color: rgba(0, 122, 204, 0.5);
}
.emoji {
height: 1.4em;
}
code {
font-size: 14px;
line-height: 19px;
}
/* for inline code */
:not(pre):not(.hljs) > code {
color: #C9AE75; /* Change the old color so it seems less like an error */
font-size: inherit;
}
/* Page Break : use <div class="page"/> to insert page break
-------------------------------------------------------- */
.page {
page-break-after: always;
}
</style>
<script src="https://unpkg.com/mermaid/dist/mermaid.min.js"></script>
</head>
<body>
<script>
mermaid.initialize({
startOnLoad: true,
theme: document.body.classList.contains('vscode-dark') || document.body.classList.contains('vscode-high-contrast')
? 'dark'
: 'default'
});
</script>
<h1 id="1-intro">1. Intro</h1>
<p>Lesson outline</p>
<p><img src="images/l2/l2-intro.png" alt="outline"></p>
<hr>
<h1 id="2-code-write-and-run-your-first-c-program">2. CODE: Write and Run Your First C++ Program</h1>
<h2 id="write-and-run-your-first-c-program">Write and Run Your First C++ Program</h2>
<p>Now that you have a sense of what you will be building in this lesson, you can learn about the tools that you will use. To get started, it helps to know a little bit about the C++ programming language. C++ is a compiled language; there is a separate program - the compiler - that converts your code to an executable program that the computer can run. This means that running a new C++ program is normally a two step process:</p>
<p>Compile your code with a compiler.
Run the executable file that the compiler outputs.</p>
<h3 id="c-main-function">C++ <code>main()</code> function</h3>
<p>In C++, every program contains a <code>main</code> function which is executed automatically when the program is run. Every part of a C++ program is run directly or indirectly from <code>main</code> , and the most basic program that will compile in C++ is just a <code>main</code> function with nothing else.</p>
<p><code>main()</code> should return an integer (an <code>int</code> in C++), which indicates if the program exited successfully. This is specified in code by writing the return type, followed by the <code>main</code> function name, followed by empty arguments:</p>
<pre class="hljs"><code><div>int main()
</div></code></pre>
<p>The body of the <code>main()</code>, which comes after the <code>main</code> function name and arguments, is enclosed in curly brackets: <code>{</code> and <code>}</code>. In this exercise, you will write the smallest possible C++ program, which is a main function with empty body. If you have trouble, have a look at the <code>solution.cpp</code> file in the workspace below.</p>
<p>Remember that you can compile and run your program with the following:</p>
<ul>
<li>To compile, use the following command: <code>g++ main.cpp</code></li>
<li>To run, use: <code>./a.out</code></li>
</ul>
<h3 id="to-complete-this-exercise">To Complete This Exercise:</h3>
<ul>
<li>Write a <code>main</code> function in the <code>main.cpp</code> file below, and then compile and run the program. The program will not have any output, but it should compile and run without errors.</li>
</ul>
<h3 id="solution">Solution</h3>
<details>
<summary>Reveal</summary>
<p><code>main.cpp</code></p>
<pre class="hljs"><code><div>int main() {}
</div></code></pre>
</details>
<hr>
<h1 id="3-compiled-languages-vs-scripted-languages">3. Compiled Languages vs Scripted Languages</h1>
<p>In the previous exercise, you compiled your C++ program before running it. If this is the first time you've worked with a compiled language, you might be wondering why one might use a compiled language. What are the advantages and disadvantages?</p>
<p>In the next couple of videos, Bjarne compares compiled and scripted languages and discusses some of the advantages and disadvantages of each.</p>
<h2 id="advantages-and-disadvantages-of-compiled-languages">Advantages and Disadvantages of Compiled Languages</h2>
<p><a href="https://youtu.be/lLdYFFIyc60"><img src="images/l2/concept3-1.png" alt="Watch Video"></a><br>
<strong>Notes</strong>:</p>
<ul>
<li>Compiler is your best friend because
<ul>
<li>It catches errors systematically and early</li>
<li>The last bug is a an old joke!</li>
<li>The code will never run if it's not following the language rules and your own rule</li>
<li>It generates a much better code that runs 10-50 times faster than alternatives</li>
</ul>
</li>
</ul>
<h2 id="advantages-of-a-dynamically-scripted-language">Advantages of a Dynamically Scripted Language</h2>
<p><a href="https://youtu.be/DedCGNJAZQY"><img src="images/l2/concept3-2.png" alt="Watch Video"></a><br>
<strong>Notes</strong>:</p>
<ul>
<li>Advantages
<ul>
<li>Interactive</li>
<li>You don't have to wait for the compilation</li>
</ul>
</li>
<li>JavaScript (scripting language) engine is written in C++</li>
<li>Big systems don't use one language and professionals know more than a language (3-5) and pick the right one for the task</li>
</ul>
<hr>
<h1 id="4-c-output-and-language-basics">4. C++ Output and Language Basics</h1>
<h2 id="first-code-example">First Code Example</h2>
<p>The next is the first example of code that might be included in a typical C++ program. Hover your cursor over each line of the code and then click play to hear an explanation, or have a look at the <strong>Review</strong> section below.<br>
<code>main.cpp</code></p>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"Hello!"</span> << <span class="hljs-string">"\n"</span>;
}
</div></code></pre>
<p>To run the code:</p>
<pre class="hljs"><code><div>$ g++ main.cpp
$ ./a.out
</div></code></pre>
<h2 id="review">Review</h2>
<p><code>#include <iostream></code></p>
<ul>
<li>The <code>#include</code> is a preprocessor command which is executed before the code is compiled. It searches for the <code>iostream</code> header file and pastes its contents into the program. <code>iostream</code> contains the declarations for the input/output stream objects.</li>
</ul>
<p><code>using std::cout;</code></p>
<ul>
<li>Namespaces are a way in C++ to group identifiers (names) together. They provide context for identifiers to avoid naming collisions. The <code>std</code> namespace is the namespace used for the standard library.</li>
<li>The <code>using</code> command adds <code>std::cout</code> to the global scope of the program. This way you can use <code>cout</code> in your code instead of having to write <code>std::cout</code>.</li>
<li><code>cout</code> is an output stream you will use to send output to the <strong>notebook</strong> or to a terminal, if you are using one.</li>
<li>Note that the second two lines in the example end with a semicolon <code>;</code>. Coding statements end with a semicolon in C++. The <code>#include</code> statement is a preprocessor command, so it doesn't need one.</li>
</ul>
<p><code>cout << "Hello!" << "\n";</code></p>
<ul>
<li>In this line, the code is using cout to send output to the <strong>notebook</strong>. The <code><<</code> operator is the stream insertion operator, and it writes what's on the right side of the operator to the left side. So in this case, <code>"Message here"</code> is written to the output stream <code>cout</code>.</li>
</ul>
<h1 id="5-send-output-to-the-console">5. Send Output to the Console</h1>
<p><img src="images/l2/hello-screenshot.png" alt="hello"></p>
<p>Previously, you wrote the most basic program that will compile in C++: a simple <code>main()</code> . In this exercise, you will modify that program to print output to the terminal. Don't forget that you can compile and run your program with the following two steps from the command line:</p>
<pre class="hljs"><code><div>$ g++ main.cpp
$ ./a.out
</div></code></pre>
<h2 id="to-complete-this-exercise">To Complete This Exercise:</h2>
<p>Add code to the <code>main.cpp</code> file below to print "Hello!" using <code>cout</code> .</p>
<h2 id="solution">Solution</h2>
<details>
<summary>Reveal</summary>
<p><code>main.cpp</code></p>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-comment">// <span class="hljs-doctag">TODO:</span> Add your co<iostream></span>
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"Hello"</span> << <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
}
</div></code></pre>
</details>
<hr>
<h1 id="6-how-to-store-data">6. How to Store Data</h1>
<p><img src="images/l2/storing-data.png" alt="storing-data"></p>
<p>In the next few <strong>notebooks</strong>, you will learn how to store data in your program, including basic variables with primitive types and vector containers.</p>
<hr>
<h1 id="7-bjarne-introduces-c-types">7. Bjarne Introduces C++ Types</h1>
<p>C++ uses variables, just as in nearly every other programming language. Unlike some other languages, however, in C++ each variable has a fixed type. When a new variable is "declared", or introduced in a program, the program author must (usually) specify the variable type in the declaration.</p>
<p>In this next video, Bjarne discusses types for C++.</p>
<p><a href="https://youtu.be/2tuTBl584ow"><img src="images/l2/concept7.png" alt="Watch Video"></a></p>
<hr>
<h1 id="8-primitive-variable-types">8. Primitive Variable Types</h1>
<p>In the <strong>notebook</strong> below, you will learn about some of the fundamental types in C++.</p>
<h2 id="primitive-variable-types">Primitive Variable Types</h2>
<p>C++ has several "primitive" variable types, which are things like <code>int</code>s (integers), <code>string</code>s, <code>float</code>s, and others. These should be similar to variable types in other programming languages you have used.</p>
<p><strong>Note:</strong> In the cells below, variables will be declared and values assigned. In C++, once a variable has been declared, it can not be redeclared in the same scope. This means that if you try to declare a variable twice in the same function, you will see an error.</p>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-comment">// Declaring and initializing an int variable.</span>
<span class="hljs-keyword">int</span> a = <span class="hljs-number">9</span>;
<span class="hljs-comment">// Declaring a string variable without initializing right away.</span>
<span class="hljs-built_in">std</span>::<span class="hljs-built_in">string</span> b;
<span class="hljs-comment">// Initializing the string b.</span>
b = <span class="hljs-string">"Here is a string"</span>;
<span class="hljs-built_in">cout</span> << a << <span class="hljs-string">"\n"</span>;
<span class="hljs-built_in">cout</span> << b << <span class="hljs-string">"\n"</span>;
}
</div></code></pre>
<details>
<summary>Output</summary>
<pre class="hljs"><code><div>9
Here is a string
</div></code></pre>
</details>
<h3 id="practice">Practice</h3>
<p>Practice declaring an <code>int</code> with the name <code>j</code> in the cell below and assing the value <code>10</code> to <code>j</code>.</p>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-comment">// Declare and initialize j here.</span>
}
</div></code></pre>
<h3 id="solution">Solution</h3>
<details>
<summary>Reveal</summary>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">int</span> j = <span class="hljs-number">10</span>;
<span class="hljs-built_in">cout</span> << j << <span class="hljs-string">"\n"</span>;
}
</div></code></pre>
<p>Output</p>
<pre class="hljs"><code><div>10
</div></code></pre>
</details>
<hr>
<h1 id="9-what-is-a-vector">9. What is a Vector?</h1>
<p><a href="https://youtu.be/AfwagT0JJO0"><img src="images/l2/concept9.png" alt="concept9"></a></p>
<ul>
<li>The default data structure in C++</li>
<li>Vector is a linear sequence of contiguously allocated memory</li>
</ul>
<hr>
<h1 id="10-c-vectors">10. C++ Vectors</h1>
<p>In the previous concept, you learned about some of the primitive types that C++ offers, including strings and ints, and you learned how to store these types in your program. In this concept, you will learn about one of the most common data structures in C++: the <code>vector</code>.</p>
<p>In the <strong>notebook</strong> below, you will learn how to declare and store a vector containing primitive types, and you will also get some practice with 2D vectors, which you will be using in A* search.</p>
<h2 id="vector-containers">Vector Containers</h2>
<h3 id="1d-vectors">1D Vectors</h3>
<p>C++ also has several container types that can be used for storing data. We will start with <code>vector</code>s, as these will be used throughout this lesson, but we will also introduce other container types as needed.</p>
<p>Vectors are a sequence of elements of a single type, and have useful methods for getting the size, testing if the vector is empty, and adding elements to the vector.</p>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>;
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-comment">// Three ways of declaring and initializing vectors.</span>
<span class="hljs-built_in">vector</span><<span class="hljs-keyword">int</span>> v_1{<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>}; <span class="hljs-comment">// preferred</span>
<span class="hljs-built_in">vector</span><<span class="hljs-keyword">int</span>> v_2 = {<span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>};
<span class="hljs-built_in">vector</span><<span class="hljs-keyword">int</span>> v_3;
v_3 = {<span class="hljs-number">6</span>};
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"Everything worked!"</span> << <span class="hljs-string">"\n"</span>;
}
</div></code></pre>
<h3 id="2d-vectors">2D Vectors</h3>
<p>Unfortunately, there isn't a built-in way to print vectors in C++ using <code>cout</code>. You will learn how to access vector elements and you will write your own function to print vectors later. For now, you can see how vectors are created and stored. Below, you can see how to nest vectors to create 2D containers.</p>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>;
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-comment">// Creating a 2D vector.</span>
<span class="hljs-built_in">vector</span><<span class="hljs-built_in">vector</span><<span class="hljs-keyword">int</span>>> v {{<span class="hljs-number">1</span>,<span class="hljs-number">2</span>}, {<span class="hljs-number">3</span>,<span class="hljs-number">4</span>}};
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"Great! A 2D vector has been created."</span> << <span class="hljs-string">"\n"</span>;
}
</div></code></pre>
<h3 id="practice">Practice</h3>
<p>Practice declaring a <code>vector<int></code> in the cell below, and assign the value <code>{6, 7, 8}</code>.</p>
<h3 id="solution">Solution</h3>
<details>
<summary>Reveal</summary>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>;
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-comment">// Declare and initialize a vector v here.</span>
<span class="hljs-built_in">vector</span><<span class="hljs-keyword">int</span>> v{<span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>};
}
</div></code></pre>
</details>
<hr>
<h1 id="11-c-comments">11. C++ Comments</h1>
<p><a href="https://youtu.be/KU2GjMbxnXo"><img src="images/l2/concept11.png" alt="concept11"></a></p>
<p>You may have noticed comments in some of the code up until this point. C++ provides two kinds of comments:</p>
<pre class="hljs"><code><div><span class="hljs-comment">// You can use two forward slashes for single line comments.</span>
<span class="hljs-comment">/*
For longer comments, you can enclose the text with an opening
slash-star and closing star-slash.
*/</span>
</div></code></pre>
<hr>
<h1 id="12-using-auto">12. Using Auto</h1>
<p>You have now seen how to store basic types and vectors containing those types. As you practiced declaring variables, in each case you indicated the type of the variable. It is possible for C++ to do automatic type inference, using the <code>auto</code> keyword.</p>
<p>Have a look at the <strong>notebook</strong> below to see how this works.</p>
<h2 id="using-auto">Using <code>auto</code></h2>
<p>In your previous code, the type for each variable was explicitly declared. In general, this is not necessary, and the compiler can determine the type based on the value being assigned. To have the type automatically determined, use the <code>auto</code> keyword. You can test this by executing the cell below:</p>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>;
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">auto</span> i = <span class="hljs-number">5</span>;
<span class="hljs-keyword">auto</span> v_6 = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>};
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"Variables declared and initialized without explicitly stating type!"</span> << <span class="hljs-string">"\n"</span>;
}
</div></code></pre>
<details>
<summary>Output</summary>
<pre class="hljs"><code><div>Variables declared and initialized without explicitly stating type!
</div></code></pre>
</details>
<p><strong>Note</strong>: It is helpful to manually declare the type of a variable if you want the variable type to be clear for reader of your code, or if you want to be explicit about the number precision being used; C++ has several number types with different levels of precision, and this precision might not be clear from the value being assigned.</p>
<h3 id="practice">Practice</h3>
<p>Practice using <code>auto</code> to declare and initialize a vector <code>v</code> with the value <code>{7, 8, 9, 10}</code>. If you have trouble, <strong>click here</strong> for help.</p>
<h3 id="solution">Solution</h3>
<details>
<summary>Reveal</summary>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>;
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-comment">// Declare and initialize v using auto here.</span>
<span class="hljs-keyword">auto</span> v = {<span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>, <span class="hljs-number">10</span>};
}
</div></code></pre>
</details>
<h2 id="on-to-an-exercise">On to an Exercise</h2>
<p>Now that you have seen some exposure to variables and containers, test your knowledge in the next exercise! Before you go, be sure to have a careful look at the 2D vector example back in Storing Vectors, as you'll need this for the exercise.</p>
<hr>
<h1 id="13-store-a-grid-in-your-program">13. Store a Grid in Your Program</h1>
<p><img src="images/l2/a-search-algorithm-1.png" alt="a-search"></p>
<p>In order to write the A* search algorithm, you will need a grid or "board" to search through. We'll be working with this board throughout the remaining exercises, and we'll start by storing a hard-coded board in the <code>main</code> function. In later exercises, you will write code to read the board from a file.</p>
<h2 id="to-complete-this-exercise">To Complete This Exercise:</h2>
<ol>
<li>In the <code>main</code> function, declare a variable <code>board</code> as a vector of vectors of ints: <code>vector<vector<int>></code>.</li>
<li>Assign this data to the <code>board</code> variable:<pre class="hljs"><code><div>{{0, 1, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0}}
</div></code></pre>
</li>
</ol>
<p><strong>Note</strong>: you will need to include the <code>vector</code> library, just as iostream is included. You will also need to use the namespace <code>std::vector</code> if you want to write <code>vector</code> rather than <code>std::vector</code> in your code.</p>
<h2 id="solution">Solution</h2>
<details>
<summary>Reveal</summary>
<p><code>main.cpp</code></p>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-comment">// <span class="hljs-doctag">TODO:</span> Declare a "board" variable here, and store</span>
<span class="hljs-comment">// the data provided above.</span>
<span class="hljs-built_in">vector</span><<span class="hljs-built_in">vector</span><<span class="hljs-keyword">int</span>>> board = {{<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>}, \
{<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>}, \
{<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>}, \
{<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>}, \
{<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>}};
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"Hello!"</span> << <span class="hljs-string">"\n"</span>;
}
</div></code></pre>
</details>
<hr>
<h1 id="14-getting-ready-for-printing">14. Getting Ready for Printing</h1>
<p><img src="images/l2/printer.png" alt="printing"></p>
<p>Excellent work! In the next part of the mini-project, you will write a function to print out the board that you have stored. Before you can do that, you will need to learn more about several important parts of the C++ language.</p>
<p>In the next exercises, you will learn how to access elements in the vectors you stored previously. Additionally, you will learn to write loops in C++. Finally, you will learn how to write C++ functions so you can begin expanding your project.</p>
<hr>
<h1 id="15-working-with-vectors">15. Working with Vectors</h1>
<p>You declared and initialized vectors in a previous <strong>notebook</strong>, but in order for the vector to be useful, you will need to be able to retrieve the vector elements. You will learn about vector access in this <strong>notebook</strong>, along with some other useful vector features.</p>
<h2 id="1d-vector-access">1D Vector Access</h2>
<p>To begin, it is helpful to know how to access vector elements of an existing vector. Execute the cells below to see how this can be done:</p>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>;
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-built_in">vector</span><<span class="hljs-keyword">int</span>> a = {<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>};
<span class="hljs-built_in">cout</span> << a[<span class="hljs-number">0</span>] << <span class="hljs-string">", "</span>;
<span class="hljs-built_in">cout</span> << a[<span class="hljs-number">1</span>] << <span class="hljs-string">", "</span>;
<span class="hljs-built_in">cout</span> << a[<span class="hljs-number">2</span>];
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"\n"</span>;
}
</div></code></pre>
<details>
<summary>Output</summary>
<pre class="hljs"><code><div>0, 1, 2
</div></code></pre>
</details>
<p>Great! Now try accessing some of the elements of vector <code>a</code> yourself</p>
<p>If you tried to access the elements of <code>a</code> using an out-of-bound index, you might have noticed that there is no error or exception thrown. If you haven't seen this already, try the following code in the cell above to see what happens:<br>
<code>cout << a[10];</code></p>
<details>
<summary>Output</summary>
<pre class="hljs"><code><div>0
</div></code></pre>
</details>
<p>In this case, <em>the behavior is undefined</em>, so you can not depend on a certain value to be returned. Be careful about this!</p>
<p>In a later lesson where you will learn about exceptions, we will discuss other ways to access vector elements that don't fail silently with out-of-range indices.</p>
<h2 id="2d-vector-access">2D Vector Access</h2>
<p>In the previous exercise, you stored a 2D vector - a <code>vector<vector<int>></code>. The syntax for accessing elements of a 2D vector is very similar to accessing in a 1D vector. In the second cell below, try accessing an element of <code>b</code>.</p>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>;
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-built_in">vector</span><<span class="hljs-built_in">vector</span><<span class="hljs-keyword">int</span>>> b = {{<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>},
{<span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>},
{<span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>}};
<span class="hljs-built_in">cout</span> << b[<span class="hljs-number">0</span>][<span class="hljs-number">0</span>] << <span class="hljs-string">"\n"</span>;<span class="hljs-built_in">cout</span> << b[<span class="hljs-number">0</span>][<span class="hljs-number">1</span>] << <span class="hljs-string">"\n"</span>;<span class="hljs-built_in">cout</span> << b[<span class="hljs-number">0</span>][<span class="hljs-number">2</span>] << <span class="hljs-string">"\n"</span>;<span class="hljs-built_in">cout</span> << b[<span class="hljs-number">2</span>][<span class="hljs-number">0</span>] << <span class="hljs-string">"\n"</span>;
<span class="hljs-built_in">cout</span> << b[<span class="hljs-number">2</span>][<span class="hljs-number">2</span>] << <span class="hljs-string">"\n"</span>;
<span class="hljs-built_in">cout</span> << b[<span class="hljs-number">2</span>][<span class="hljs-number">3</span>] << <span class="hljs-string">"\n"</span>;
}
</div></code></pre>
<details>
<summary>Output</summary>
<pre class="hljs"><code><div>1
1
2
3
2
3
</div></code></pre>
</details>
<h2 id="getting-a-vectors-length">Getting a Vector's Length</h2>
<h3 id="1d-vector-length">1D Vector Length</h3>
<p>One method of a <code>vector</code> object that will be useful in the next code exercise is the <code>.size()</code> method. This returns the length of the vector. Execute the cell below to see how this can be used:</p>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>;
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-built_in">vector</span><<span class="hljs-keyword">int</span>> a = {<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>};
<span class="hljs-comment">// Print the length of vector a to the console.</span>
<span class="hljs-built_in">cout</span> << a.size() << <span class="hljs-string">"\n"</span>;
}
</div></code></pre>
<details>
<summary>Output</summary>
<pre class="hljs"><code><div>5
</div></code></pre>
</details>
<h3 id="2d-vector-length">2D Vector Length</h3>
<p>For the <code>vector<vector<int>></code> <code>b</code> defined above, try to get the size of one of the inner vectors - this should be 4. If you have trouble, click the button below for some help.</p>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>;
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-built_in">vector</span><<span class="hljs-built_in">vector</span><<span class="hljs-keyword">int</span>>> b = {{<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>},
{<span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>},
{<span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>}};
<span class="hljs-comment">// Print the length of an inner vector of b here.</span>
<span class="hljs-built_in">cout</span> << b[<span class="hljs-number">0</span>].size() << <span class="hljs-string">"\n"</span>;
}
</div></code></pre>
<details>
<summary>Output</summary>
<pre class="hljs"><code><div>4
</div></code></pre>
</details>
<p>Nice work! You now know a little more about C++ vectors. After learning about for loops, you should be well prepared for the upcoming code exercises.</p>
<hr>
<h1 id="16-for-loops">16. For Loops</h1>
<p>Just as in other languages you've worked with, C++ has both <code>for</code> loops and <code>while</code> loops. You will learn about <code>for</code> loops in the <strong>notebook</strong> below, and you will see <code>while</code> loops later in the course.</p>
<h2 id="for-loop-with-an-index-variable">For Loop with an Index Variable</h2>
<p>A simple <code>for</code> loop using an index variable has the following syntax. Click the button below for an explanation of the different parts.</p>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>; i < <span class="hljs-number">5</span>; i++) {
<span class="hljs-built_in">cout</span> << i << <span class="hljs-string">"\n"</span>;
}
}
</div></code></pre>
<details>
<summary>Output</summary>
<pre class="hljs"><code><div>0
1
2
3
4
</div></code></pre>
</details>
<h3 id="the-increment-operator">The Increment Operator</h3>
<p>If you haven't seen the <code>++</code> operator before, this is the <em>post-increment operator</em>, and it is where the <code>++</code> in the name "C++" comes from. The operator increments the value of <code>i</code>.</p>
<p>There is also a <em>pre-increment operator</em> which is used before a variable, as well as <em>pre</em> and <em>post decrement</em> operators: <code>--</code>. The difference between <em>pre</em> and <em>post</em> lies in what value is returned by the operator when it is used.</p>
<p>You will only use the <em>post-increment operator</em> <code>i++</code> for now, but if you are curious, click below for an explanation of the code:</p>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">auto</span> i = <span class="hljs-number">1</span>;
<span class="hljs-comment">// Post-increment assigns i to c and then increments i.</span>
<span class="hljs-keyword">auto</span> c = i++;
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"Post-increment example:"</span> << <span class="hljs-string">"\n"</span>;
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"The value of c is: "</span> << c << <span class="hljs-string">"\n"</span>;
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"The value of i is: "</span> << i << <span class="hljs-string">"\n"</span>;
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"\n"</span>;
<span class="hljs-comment">// Reset i to 1.</span>
i = <span class="hljs-number">1</span>;
<span class="hljs-comment">// Pre-increment increments i, then assigns to c.</span>
c = ++i;
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"Pre-increment example:"</span> << <span class="hljs-string">"\n"</span>;
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"The value of c is: "</span> << c << <span class="hljs-string">"\n"</span>;
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"The value of i is: "</span> << i << <span class="hljs-string">"\n"</span>;
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"\n"</span>;
<span class="hljs-comment">// Decrement i;</span>
i--;
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"Decrement example:"</span> << <span class="hljs-string">"\n"</span>;
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"The value of i is: "</span> << i << <span class="hljs-string">"\n"</span>;
}
</div></code></pre>
<details>
<summary>Output</summary>
<pre class="hljs"><code><div>Post-increment example:
The value of c is: 1
The value of i is: 2
Pre-increment example:
The value of c is: 2
The value of i is: 2
Decrement example:
The value of i is: 1
</div></code></pre>
</details>
<h3 id="practice">Practice</h3>
<p>Before you learn how to write a <code>for</code> loop using an iterator, practice writing a for loop that prints values from <code>-3</code> through <code>10</code> in the cell below. Don't forget to assign an initial value (like 0) to your index variable!</p>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-comment">// Add your code here.</span>
}
</div></code></pre>
<details>
<summary>Solution</summary>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-comment">// Add your code here.</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">-3</span>; i < <span class="hljs-number">11</span>; i++)
{
<span class="hljs-built_in">cout</span> << i << <span class="hljs-string">"\n"</span>;
}
}
</div></code></pre>
<details>
<summary>Output</summary>
<pre class="hljs"><code><div>-3
-2
-1
0
1
2
3
4
5
6
7
8
9
10
</div></code></pre>
</details>
</details>
<h2 id="for-loop-with-a-container">For Loop with a Container</h2>
<p>C++ offers several ways to iterate over containers. One way is to use an index-based loop as above. Another way is using a "range-based loop", which you will see frequently in the rest of this course. See the following code for an example of how this works:</p>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-comment">// Add your code here.</span>
<span class="hljs-built_in">vector</span><<span class="hljs-keyword">int</span>> a {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>};
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i: a) {
<span class="hljs-built_in">cout</span> << i << <span class="hljs-string">"\n"</span>;
}
}
</div></code></pre>
<details>
<summary>Output</summary>
<pre class="hljs"><code><div>1
2
3
4
5
</div></code></pre>
</details>
<h3 id="challenge">Challenge</h3>
<p>In the next cell, try to write a double range-based for loop that prints all of the entries of the 2D vector <code>b</code>. If you get stuck, click on the solution button for an explanation.</p>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-comment">// Add your code here.</span>
<span class="hljs-built_in">vector</span><<span class="hljs-built_in">vector</span><<span class="hljs-keyword">int</span>>> b {{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>},
{<span class="hljs-number">3</span>, <span class="hljs-number">4</span>},
{<span class="hljs-number">5</span>, <span class="hljs-number">6</span>}};
<span class="hljs-comment">// Write your double loop here.</span>
}
</div></code></pre>
<details>
<summary>Solution</summary>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-comment">// Add your code here.</span>
<span class="hljs-built_in">vector</span><<span class="hljs-built_in">vector</span><<span class="hljs-keyword">int</span>>> b {{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>},
{<span class="hljs-number">3</span>, <span class="hljs-number">4</span>},
{<span class="hljs-number">5</span>, <span class="hljs-number">6</span>}};
<span class="hljs-comment">// Write your double loop here.</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> i: b)
{
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j: i)
{
<span class="hljs-built_in">cout</span> << j << <span class="hljs-string">", "</span>;
}
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"\n"</span>;
}
}
</div></code></pre>
<details>
<summary>Output</summary>
<pre class="hljs"><code><div>1, 2,
3, 4,
5, 6,
</div></code></pre>
</details>
</details>
<hr>
<h1 id="17-functions">17. Functions</h1>
<p>The last thing you will need to learn in order to complete the next exercise is how to write a function. Fortunately, you have seen a function before when you wrote <code>main()</code>!</p>
<p>When a function is declared and defined in a single C++ file, the basic syntax is as follows:</p>
<pre class="hljs"><code><div><span class="hljs-function">return_type <span class="hljs-title">FunctionName</span><span class="hljs-params">(parameter_list)</span> </span>{
<span class="hljs-comment">// Body of function here.</span>
}
</div></code></pre>
<p>See the following <strong>notebook</strong> for examples:</p>
<h2 id="functions">Functions</h2>
<p>In the cell below, there is a simple function to add two numbers and return the result. Test the code below, and click the button for a more in-depth explanation.</p>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
<span class="hljs-comment">// Function declared and defined here.</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">AdditionFunction</span><span class="hljs-params">(<span class="hljs-keyword">int</span> i, <span class="hljs-keyword">int</span> j)</span>