You are on page 1of 36

6.

S096: Introduction to C/C++


Frank Li, Tom Lieber, Kyle Murray
Lecture 8:
Last Lecture
Helter Skelter Fun!
January 31, 2012
1
Today

Stanar Tem!late Library "STL#

$ra%ay $onst

&'ce!tions

Function !ointers

(rie) intro to $**11
2


Today

Standard Template Library (STL)

$ra%ay $onst

&'ce!tions

Function !ointers

(rie) intro to $**11
3


Standard Template Library (STL)

+,nclue- ./ com!iler

$ontains containers/ata structures, iterators,
an al0orit1ms

1tt!2//...3c!lus!lus3com/re)erence
4




ector!

&4ui5alent to array lists in ot1er lan0ua0es
"ynamic si%e#
6inclue 75ector8
9
st225ector7int8 int:list;
int:list3!us1:back"1#;
int tm! < int:list=0>; // tm! < 1
int:list3!o!:back"#; // int:list no. em!ty
5






"ap

Like a ictionary in ?yt1on "tree
im!lementation#3
6inclue 7ma!8
9
st22ma!7c1ar, int8 letter:to:int;
letter:to:int=@a@> < 1;
letter:to:int=@b@> < 2;
int !os < letter:to:int=@a@> // !os < 1;
6
00
01
02
03
13
14
15
: :
521-9655
521-8976
521-1234
Keys
Hash
Function
Buckets
John Smith
Lisa Smith
Sandra Dee
Image by MIT OpenCourseWare.








#t$er! Container!
At1er use)ul containers2

<array>: array ./ )unctions

<list>: oubly linke list

<set>: stores only uni4ue elements, tree


im!lementation

<unordered_map>: actual 1as1 table

<unordered_set>: stores only uni4ue


elements, 1as1 table im!lementation

Look online for more!


7





Iterator!

AbBect t1at !oints to elements in a ata


structure, an can iterate t1rou013 Like a
!ointer3

Cector iterator2 st225ector7int822iterator it;

Dccess element at iterator2 Eit;

$an o a/subtract to iterators2 it**, itFF


8









ector Iterator!
6inclue 75ector8
6inclue 7iostream8
333
st225ector7int8 5ec;
)or "int i<1; i7<G; i**#
5ec3!us1:back"i#;
)or "st225ector7int822iterator it < 5ec3be0in"#;
it !< 5ec3en"#; **it#H
st22cout 77 @ @ 77 Eit;
I
//Jill !rint2 1 2 3 K G
9







Cool/%!e&ul 'l(orit$m

6inclue 7al0orit1m8

Sort a 5ector2

st22sort"5ec3be0in"#, 5ec3en"##;

Le5erse a 5ector2

st22re5erse"5ec3be0in"#, 5ec3en"##;

Min/Ma'2

st22min"3,1# << 1 ; st22ma'"3,1# << 3

So many more online!


10



Today
Stanar Tem!late Library "STL#
Crazay Const
&'ce!tions
Function !ointers
(rie) intro to $**11
11






Con!t "adne!!)

const int ' << int const ';


// $onst int

const int E c1;


// ?ointer to const int

int const E c2; // Same as c1

int E const c3; // $onst !ointer to 5ariable int


12




"ore Con!t "adne!!)

$onst in )unction !arameter


bool cant:touc1:t1is"const myobB M obB#;
//cant:touc1:t1is can@t moi)y obB

$onst )unctions are sa)e )or const obBects, but can


be calle by all obBects3 NonFconst )unctions can
only be calle by nonFconst obBects3
bool catc1:c1an0e:t1is"# const H
333
I
13


*$en +ill t$e madne!! end),),
const int* const
crazay(const int* const & madness) const
14



Today
Stanar Tem!late Library "STL#
$ra%ay $onst
!"ceptions
Function !ointers
(rie) intro to $**11
15


-.ception!

&'ce!tions are +error- obBects t1at are +t1ro.n-


.1en t1in0s 0o ba3
16






-.ception!

&'ce!tion !arent obBect in st22e'ce!tion

st22runtime:error

st22ba:alloc

st22ba:cast

$an create your o.n custom e'ce!tions


class My&'ce!tion 2 !ublic e'ce!tionH
const c1ar E .1at"# const H
return OMy&'ce!tionO; // 1umanFreaable
I
I;
17







Try Catc$
try H
9 // ?rotecte coe
t1ro. My&rror"#;
Icatc1" Pour&rror e1 #H
cout 77 e13.1at"# 77 enl;
Icatc1" My&rror e2 #H
cout 77 e23.1at"# 77 enl;
Icatc1"333# H
9 // 1anle all ot1er e'ce!tions
I
18






T$ro+in( t$at -.ception!

$an t1ro. a !rimiti5e as an e'ce!tion


tryH
333
t1ro. 20;
Icatc1"int e# H cout 77 +&rror2 - 77 e 77 enl; I

(est .ay to catc1 your o.n e'ce!tion2


tryH
t1ro. My&'ce!tion"#;
Icatc1"My&'ce!tion M e# H
cout 77 e3.1at"# 77 enl;
I
19



/unction! T$ro+in(

Functions a t1ro."exceptions-thrown# at en
o) eclaration3
5oi a11"int i# t1ro."# H
//T1is )unction assume to not t1ro. anyt1in0
I
5oi bla11"int i# t1ro."int# H
//T1is )unction may t1ro. int i) t1ere@s an error
I
20



Today
Stanar Tem!late Library "STL#
$ra%ay $onst
&'ce!tions
#unction pointers
(rie) intro to $**11
21



/unction! 0ointer!

5oi "E)oo# "int#; // Foo is !ointer to 5oi


)unction t1at takes 1 int ar0ument3

5oi E"E)oo#"int E, c1ar#; //Dny 0uessesQ


22





/unction! 0ointer!

5oi "E)oo# "int#; // Foo is !ointer to 5oi


)unction t1at takes 1 int ar0ument3

5oi E"E)oo#"int E, c1ar#; //Foo is a !ointer to a


)unction t1at takes 1 intE ar0ument an 1 c1ar
ar0ument, t1at returns 5oiE
23










%!in( /unction! 0ointer!
6inclue 7iostream8
5oi my:int:)unc"int '#H
st22cout 77 ' 77 st22enl;
I
int main"#H
5oi "E)oo#"int#;
)oo < Mmy:int:)unc; // T1e am!ersan is actually o!tional
"E)oo#"2#; // $alls my:int:)unc
)oo"2#; // Same as abo5e line
return 0;
I
24







/unction! 0ointer! 'r(ument!
6inclue 7iostream8
5oi call"int '#H
st22cout 77 ' 77 st22enl;
I
5oi call:me:maybe"int number, 5oi "Ecall:)unc#"int## H
call:)unc"number#;
I
int main"#H
5oi "E)oo#"int#;
)oo < call;
call_me_maybe($%%& 'oo)
return 0;
I
25






/unction! 0ointer! 'r(ument!
6inclue 7iostream8
5oi call"int '#H
st22cout 77 ' 77 st22enl;
I
5oi call:me:maybe"int number, 5oi "Ecall:)unc#"int## H
call:)unc"number#;
I
int main"#H
call_me_maybe($%%& call)
return 0;
I
26





/or1eac$
6inclue 7al0orit1m8 // Heaer )ile nee )or )or:eac1"#
6inclue 75ector8
6inclue 7iostream8
usin0 names!ace st;
5oi !rint "int i# H cout 77 @ @ 77 i; I
int main"#H
5ector7int8 my5ector;
my5ector3!us1:back"10#;
my5ector3!us1:back"20#;
)or:eac1 "my5ector3be0in"#, my5ector3en"#, !rint#;
333
I // ?rints out 10 20
27



Today
Stanar Tem!late Library "STL#
$ra%ay $onst
&'ce!tions
Function !ointers
(rie' intro to C))%%
28




C++22

Latest an +0reatest- stanar o) $** ./ )ancy


)eatures

Fancy )eature 12 Duto ty!in0


int ' < 1;
auto y < ';
5ector7int8 5ec;
auto itr < 5ec3be0in"#; //5ersus 5ector7int822iterator
29



C++22

Latest an +0reatest- stanar o) $** ./ )ancy


)eatures

Fancy )eature 22 Reclare ty!in0


int ' < 1;
eclty!e"'# y < '; //Makes y t1e same ty!e as '
30




C++22

Latest an +0reatest- stanar o) $** ./ )ancy


)eatures

Fancy )eature 32 Li01t an0lebrackets

(e)ore $**112
5ector75ector7int8 8 5ector:o):int:5ectors;

$**112
5ector75ector7int88 5ector:o):int:5ectors;
31





C++22

Latest an +0reatest- stanar o) $** ./ )ancy


)eatures

Fancy )eature K2 Lan0e )or loo!s


5ector7int8 5ec;
5ec3!us1:back" 1 #;
5ec3!us1:back" 2 #;
)or "intM i 2 5ec # H
i**; // increments t1e 5alue in t1e 5ector
I
32







C++22

Latest an +0reatest- stanar o) $** ./ )ancy )eatures

Fancy )eature G2 Lamba )unctions!!!

Synta'2 => "ar0:list# H )unc:e)inition I

&'2
6inclue 7iostream8
usin0 names!ace st;
int main"#H
auto )unc < => "int i# H cout 77 OHi O 77 i 77 enl; I;
)unc"1#; // no. call t1e )unction
I
33



C++22

Many more )ancy )eatures "stron0ly ty!e


enums, null !ointer constant, etc#!
S"T:T# /
Pay!!!

Le)erence2
1tt!2//...3c!ro0rammin03com/tutorial31tml6c**11
1tt!2//en3.iki!eia3or0/.iki/$U2(U2(11
34



It3! about t$at time...

,ntrouce you to $ an $** synta', )eatures, an


ioms3

Va5e you some e'!erience ./ .ritin0 $/$** coe

Muc1 more to learn, best .ay is to sim!ly coe more!


35
MIT OpenCourseWare
http://ocw.mit.edu
6.S096 Introduction to C and C++
IAP 2013
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

You might also like