8/12/2019 MIT6_096IAP11_lec04
1/6
6.096 Introduction to C++ January 10, 2011Massachusetts Institute of Technology John Marrero
Lecture 4 Notes: Arrays and Strings
1 Arrays
So far e ha!e used !aria"les to store !alues in #e#ory for later reuse. $e no e%&lore a#eans to store multiple !alues together as one unit, the array.
'n array is a fi%ed nu#"er of elements of the sa#e ty&e stored se(uentially in #e#ory.Therefore, an integer array holds so#e nu#"er of integers, a character array holds so#enu#"er of characters, and so on. The si)e of the array is referred to as its dimension. Todeclare an array in C++, e rite the folloing*
type arrayName[dimension];To declare an integer array na#ed arr of four ele#ents, e rite intarr[4];
The ele#ents of an array can "e accessed "y using an index into the array. 'rrays in C++ are)eroinde%ed, so the first ele#ent has an inde% of 0. So, to access the third ele#ent in arr, e
rite arr[2]; The !alue returned can then "e used ust li-e any other integer.
i-e nor#al !aria"les, the ele#ents of an array #ust "e initiali)ed "efore they can "e used/otherise e ill al#ost certainly get une%&ected results in our &rogra#. There are se!eral
ays to initiali)e the array. ne ay is to declare the array and then initiali)e so#e or all ofthe ele#ents*
intarr[4];arr[0] = 6;arr[1] = 0;arr[2] = 9;arr[3] = 6;'nother ay is to initiali)e so#e or all of the !alues at the ti#e of declaration*
intarr[4] = { 6, 0, 9, 6 };So#eti#es it is #ore con!enient to lea!e out the si)e of the array and let the co#&ilerdeter#ine the arrays si)e for us, "ased on ho #any ele#ents e gi!e it*
intarr[] = { 6, 0, 9, 6, 2, 0, 1, 1 };ere, the co#&iler ill create an integer array of di#ension 3.
The array can also "e initiali)ed ith !alues that are not -non "eforehand*
1 #include2 usin names!acestd;3
4 intmain" {$6 intarr[4];% cout
8/12/2019 MIT6_096IAP11_lec04
2/6
12
13
cout
8/12/2019 MIT6_096IAP11_lec04
3/6
9 t.oimrra-[0][3] = 6;10 t.oimrra-[1][0] = 2;11 t.oimrra-[1][1] = 0;12 t.oimrra-[1][2] = 1;13 t.oimrra-[1][3] = 1;141$ *or"inti = 0; i < 2; i++16 *or"int = 0; < 4; ++1% cout
8/12/2019 MIT6_096IAP11_lec04
4/6
The indi!idual characters in a string can "e #ani&ulated either directly "y the &rogra##er or"y using s&ecial functions &ro!ided "y the CC++ li"raries. These can "e included in a &rogra#through the use of the #include directi!e. f &articular note are the folloing*
ccty&e cty&e.h7* character handling cstdio stdio.h7* in&utout&ut o&erations cstdli" stdli".h7* general utilities cstring string.h7* string #ani&ulation
ere is an e%a#&le to illustrate the ccty&e li"rary*
1 #include2 #include3 usin names!acestd;4$ intmain" {6 c/armess-trin[] = t6709s6i999a9?@A;%) c/arcurrent = mess-trin[0];9 *or"inti = 0; current 8= :0:; current = mess-trin[++i] {10 i*"isal!/a"current 11 cout
8/12/2019 MIT6_096IAP11_lec04
5/6
This e%a#&le creates and initiali)es to strings, *rament1 and *rament2. *rament3 is
declared "ut not initiali)ed. *inaltrin is &artially initiali)ed ith ust the null character7.
*rament1 is co&ied into *rament3 using strc!-, in effect initiali)ing *rament3 to:m a s.
strcat is then used to concatenate *rament3 onto *inaltrin the function o!errites the
e%isting null character7, there"y gi!ing *inaltrin the sa#e contents as *rament3. Then
strcat is used again to concatenate *rament2 onto *inaltrin. *inaltrin is dis&layed,
gi!ing :m a strin8.
;ou are encouraged to read the docu#entation on these and any other li"raries of interest tolearn hat they can do and ho to use a &articular function &ro&erly. ne source is
htt&*.c&lus&lus.co#reference.7
http://www.cplusplus.com/reference/http://www.cplusplus.com/reference/8/12/2019 MIT6_096IAP11_lec04
6/6
MIT OpenCourseWare
http://ocw.mit.edu
6.096 Introduction to C++
January (IAP) 2011
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
http://ocw.mit.edu/http://ocw.mit.edu/termshttp://ocw.mit.edu/http://ocw.mit.edu/termsTop Related